我有两张数据。一
Sheets("Expenses")
在“行”和“列”中填充了我的所有账单以及它们所关联的帐户。只有当特定行的费用在该日期到期时,才会填充日期下方的单元格。
在第二个
Sheets("Totals")
,我在行中列出了每个帐户,在列中连续列出了日期,与
表格(“费用”)
我想做的是
WorksheetFunction.SumIf
在“表格”(“总计”)中填入每个日期每个账户的总金额。
在我下面的代码中有一点多余的代码,但这只是因为我还没有让它工作或优化。
我遇到的问题是运行时错误13:行上的类型不匹配
accSum = WorksheetFunction.SumIf(objSum, objExpAcc, strAccount)
。我认为我正确地遵循了MSDN示例,但由于某些原因,我无法使其发挥作用。
我不知道这有什么不同
表格(“费用”)
前两个
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