VBA通过变量生成公式

时间:2013-06-07 10:19:35

标签: excel vba excel-vba

有人可以帮我修一下子公式。我需要通过变量输入日期,但它总是给我一个错误'13'数据类型

我正在谈论这个问题:

Cells(5, field).FormulaLocal = "=SUMMEWENNS(Rawdata!K2:K3446;Rawdata!I2:I3446;""bezahlt"";Rawdata!A2:A3446;" >= " & weekstart & "";Rawdata!A2:A3446;" <= " & weekend & "")"

除了那个公式之外的Sub工作.....

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).FormulaLocal = "=SUMMEWENNS(Rawdata!K2:K3446;Rawdata!I2:I3446;""bezahlt"";Rawdata!A2:A3446;" >= " & weekstart & "";Rawdata!A2:A3446;" <= " & weekend & "")"

    Next
End Sub

谢谢

1 个答案:

答案 0 :(得分:0)

我建议您转换为长(或如果您需要时间加倍)

Cells(5, field).FormulaLocal = "=SUMMEWENNS(Rawdata!K2:K3446;Rawdata!I2:I3446;""bezahlt"";Rawdata!A2:A3446;"">=" & CLng(weekstart) & """;Rawdata!A2:A3446;""<=" & CLng(weekend) & """)"