0

我找不到错误。错误消息给我“运行时错误'13'”数据类型不匹配。错误似乎在函数“fillcalweeks”中。

Fillcalweeks 应该返回一个数组,其中填充了位于工作表中的开始日期和结束日期。

这是我的代码:

'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

谢谢帮助

4

2 回答 2

1

您在线上遇到错误,calweeks = fillcalweeks(weeks)因为您正在分配一个String数组(函数的结果fillcalweeksVariant.

您会注意到您声明了除calweeks. 由于 VBA 没有对此变量的显式声明,因此将其分配给Variant.

要解决此问题,请首先将其放在Option Explicit每个模块的顶部。当您编译项目时,它会提醒您这些错误(调试 -> 编译 VBA 项目)。然后,您需要做的就是声明calweeksString().

您还有第二个问题,那就是您试图在 fillcalweeks 内将Date数据类型存储在数组中。String()您需要将datestart,dateendcalweek变量转换为Strings(您可以使用该VBA.CStr()函数来执行此操作)或更改该函数fillcalweeks以返回一个Date数组。

最后,您需要为returnarray()within声明一个大小范围fillcalweeks。VBA 需要知道它有多大才能填充这些值。由于您知道有多少行(它是函数的输入),这就像替换声明一样简单returnarraywith ReDim returnarray(0 To weeks - 1, 1 to 3) As String。请注意,您要将数组的维度设置为,weeks - 1因为您的底数为零而不是一。

另一个错误是,当您输出回工作表时,需要循环,For i = 0 To weeks - 1否则数组将超出范围......

于 2013-06-07T09:45:24.540 回答
0

谢谢。我发现了代码中的所有错误。1. calweeks 的声明和 2. 数组维度:

'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
于 2013-06-07T10:06:11.937 回答