我有两张数据表。一个Sheets("Expenses")
填充了我的所有账单以及它们在行中关联的帐户,所有列都是连续的日期列表。仅当该特定行的费用在该日期到期时,才会填充日期下方的单元格。
在第二个Sheets("Totals")
中,我将每个帐户列在行中,并将日期连续列在列中,就像Sheets("Expenses")
. 我想要做的是使用WorksheetFunction.SumIf
每个日期每个帐户的总金额填充`Sheets(“Totals”)上的单元格。
我在下面有一些冗余代码,但这只是因为我还没有得到这个工作或优化。
我遇到的问题是 Run-Time Error 13: Type Mismatch on the line 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