VBA Excel - 运行时错误13使用WorksheetFunction.SumIF时类型不匹配

时间:2014-03-01 05:03:44

标签: excel-vba runtime-error worksheet-function type-mismatch sumifs

我有两张数据表。一个Sheets("Expenses")填充了我的所有账单以及它们在行中关联的账户,所有列都是连续的日期列表。只有当该特定行的费用在该日期到期时,日期下方的单元格才会被填充。

在第二个Sheets("Totals")中,我在“行”和“日期”中列出的每个帐户都在列中连续列出,与Sheets("Expenses")非常相似。我想要做的是使用WorksheetFunction.SumIf填充`Sheets(“Totals”)上的单元格,每个日期的每个帐户的总金额。

我在下面的内容中有一些冗余代码,但这只是因为我还没有完成这项工作或进行优化。

我遇到的问题是运行时错误13:在行accSum = WorksheetFunction.SumIf(objSum, objExpAcc, strAccount)上输入不匹配。我认为我正确地遵循了MSDN示例,但由于某种原因我无法使其工作。

我不知道它是否有任何区别,但在Sheets("Expenses")上,前两个Rows是包含日期和字符串混合的标题行。

Public Sub upAccRows()
Dim startDate As Date
Dim endDate As Date
Dim dExp As Integer
Dim stDateCol As Integer
Dim lDateCol As Integer
Dim lExp As Integer
Dim strAccount As String
Dim lRow As Integer
Dim currentDate As Date
Dim objSum As Range
Dim objExpAcc As Range
Dim accSum As Integer
Dim strTest As String

startDate = Worksheets("Summary").Range("B2").Value
endDate = Worksheets("Summary").Range("B1").Value
lRow = Worksheets("Expenses").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'Last row of entries

With Sheets("Totals")
    startExp = .Cells.Find("Expenses", SearchOrder:=xlByRows, SearchDirection:=xlNext).Row + 1
    startInc = .Cells.Find("Income", SearchOrder:=xlByRows, SearchDirection:=xlNext).Row
    stDateCol = .Cells.Find(What:=startDate, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column
    lDateCol = .Cells.Find(What:=endDate, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column
    lExp = startInc - 1
End With
'~~> Loops through the rows of the Accounts on the Accounts Sheet
For CountA = startExp To lExp
    '~~> Loops through the columns of the Dates on the Accounts Sheet
    For CountB = stDateCol To lDateCol
        '~~> Set Variables for the SumIf Function and Do
        With Sheets("Totals")
            currentDate = .Cells(1, CountB).Value
            strAccount = .Cells(CountA, 2).Value
        End With
        With Sheets("Expenses")
            dExp = .Cells.Find(What:=currentDate, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column
            Set objSum = .Columns(dExp)
            Set objExpAcc = .Columns(3)
            accSum = WorksheetFunction.SumIf(objSum, objExpAcc, strAccount)
            MsgBox "accSum = " & accSum
        End With
        'Sheets("Totals").Cells(CountA, CountB).Value = accSum
        dExp = dExp + 1
    Next CountB
Next CountA

End Sub

1 个答案:

答案 0 :(得分:0)

您的参数顺序为SumIf是错误的。

功能参数是:

SUMIF(range, criteria, [sum_range])

根据您的变量名称,尝试

accSum = WorksheetFunction.SumIf(objExpAcc, strAccount, objSum)