简体   繁体   中英

VBA Excel Function Error Runtimeerror 13

I cannot find the mistake. The error msg gives me "runtime error '13'" data types don't match. The error seems to be in the function "fillcalweeks".

Fillcalweeks should return an array which is filled with start and end dates which are located in a sheet.

Here's my code:

'For every calenderweek we need the start and end dates in an array to produce the timeline

Sub get_cal_weeks()
    Dim weeks As Integer, i As Integer, col As String, weekstart As Date, weekend As Date
    'start column is D
    col = "D"
    'get amount of weeks
    weeks = countcalweeks()
    'populate array calweeks
    calweeks = fillcalweeks(weeks)
    For i = 0 To weeks

       Sheets("Kalenderwochen").Range("E" & i + 1) = calweeks(i, 1)

    Next
End Sub

Function fillcalweeks(weeks As Integer) As String()
    Dim i As Integer, datestart As Date, dateend As Date, calweek As Integer, returnarray() As String
    For i = 0 To weeks
    'date start & date end
        datestart = Sheets("Kalenderwochen").Range("A" & i + 1).Value
        dateend = Sheets("Kalenderwochen").Range("B" & i + 1).Value
        calweek = Sheets("Kalenderwochen").Range("C" & i + 1).Value
        returnarray(i, 1) = datestart
        returnarray(i, 2) = dateend
        returnarray(i, 3) = calweek
        fillcalweeks = returnarray
    Next
End Function


'Counts the calenderweeks in the Kalenderwochen sheet
Function countcalweeks() As Integer
    countcalweeks = Sheets("Kalenderwochen").Range("A2").End(xlDown).row
End Function

Thx for the help

You are getting an error on the line calweeks = fillcalweeks(weeks) because you are assigning a String array (the result of the function fillcalweeks to a Variant .

You'll notice you declared every variable except calweeks . Since VBA doesn't have an explicit declaration for this variable it assigns it to a Variant .

To fix the problem, start by putting Option Explicit at the top of every module. When you compile the project, it'll alert you to errors such as these (Debug -> Compile VBA Project). Then all you need to do is declare calweeks as a String() .

There is a second problem you have and that is that you are trying to store a Date data type in your String() array within the fillcalweeks. You either need to convert the datestart , dateend and calweek variables to Strings (you can use the VBA.CStr() function to do this) or change the function fillcalweeks to return a Date array.

Finally, you need to declare a size range for the returnarray() within fillcalweeks . VBA needs to know how big this is before it can fill the values. Since you know how many rows there are (its an input to the function) this is as simple as replacing the declaration of returnarray with ReDim returnarray(0 To weeks - 1, 1 to 3) As String . Note you want to dimension the array to weeks - 1 since you have a base of zero not one.

One more error is that when you output back to the worksheet the loop needs to be For i = 0 To weeks - 1 otherwise the array will be out of bounds...

Thx. I found all the mistakes in the code. 1. declaration of calweeks and 2. the array dimension:

'For every calenderweek we need the start and end dates in an array to produce the timeline
Sub get_cal_weeks()
    Dim weeks As Integer, i As Integer, col As String, weekstart As Date, weekend As Date, calweeks() As Variant
    'start column is D
    col = "D"
    'get amount of weeks
    weeks = countcalweeks()
    'populate array calweeks
    calweeks = fillcalweeks(weeks)
    For i = 0 To weeks
        field = i + i + 4
        weekstart = calweeks(i, 0)
        weekend = calweeks(i, 1)
        Cells(5, field) = monetary_calc_week(weekstart, weekend)
    Next
End Sub

Function fillcalweeks(weeks As Integer) As Variant()
    Dim i As Integer, datestart As Date, dateend As Date, calweek As Integer, arraysize As Integer, returnarray() As Variant
    arraysize = 52
    weeks = weeks - 2
    ReDim Preserve returnarray(arraysize, 3)

    For i = 0 To weeks
    If i > arraysize Then
        arraysize = arraysize * 2
        ReDim Preserve returnarray(arraysize, 3)

    End If
    'date start & date end
        datestart = Sheets("Kalenderwochen").Range("A" & i + 2).Value
        dateend = Sheets("Kalenderwochen").Range("B" & i + 2).Value
        calweek = Sheets("Kalenderwochen").Range("C" & i + 2).Value
        returnarray(i, 0) = datestart
        returnarray(i, 1) = dateend
        returnarray(i, 2) = calweek

    Next
    fillcalweeks = returnarray
End Function

'Counts the calenderweeks in the Kalenderwochen sheet
Function countcalweeks() As Integer
    countcalweeks = Sheets("Kalenderwochen").Range("A2").End(xlDown).row
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM