我需要你的帮助。
如果我正在运行此代码,则不会出现问题(请注意 Sheet1.Range(“F2:BL6991”))
Colnumber = Sheet2.Range("A1").End(xlToRight).Column
'MsgBox (Colnumber)
For j = 3 To Colnumber
For i = 2 To Rownumber
Sheet2.Cells(i, j).Value = Application.WorksheetFunction.SumIfs(Sheet1.Range("F2:BL6991"), Sheet1.Range("E2:E6991"), Sheet2.Cells(i, 1))
Next i
Next j
但是当我尝试更改为此代码时:(我将代码从 Sheet1.Range(“F2:BL6991”)更改为Sheet1.Range(单元格(2,j + 3),单元格(6991, j + 3)))
Colnumber = Sheet2.Range("A1").End(xlToRight).Column
'MsgBox (Colnumber)
For j = 3 To Colnumber
For i = 2 To Rownumber
Sheet2.Cells(i, j).Value = Application.WorksheetFunction.SumIfs(Sheet1.Range(cells(2,j+3), cells(6991, j+3)) , Sheet1.Range("E2:E6991"), Sheet2.Cells(i, 1))
Next i
Next j
它给了我错误1004.
即使我使用此代码,它也给了我错误:
Sheet1.Range(cells(2,6), cells(6991, 6))
或
Sheet2.Cells(i, j).Value = varResult
varResult = Application.WorksheetFunction.SumIfs( _
Arg1:=Sheet1.Range(Cells(2, 6), Cells(6991, 6)), _
Arg2:=Sheet1.Range("E2:E6991"), _
Arg3:=Sheet2.Cells(i, 1))
你能指导我做错的地方以及如何解决它吗?
TIA
答案 0 :(得分:0)
你的主要问题是SUMIF只有3个参数:
SUMIF(range, criteria, [sum_range])
这将有效:
Dim Range1 As Range
Dim Condition As Range
Set Range1 = Worksheets("Sheet1").Range("F2:BL6991")
Colnumber = Sheet2.Range("A1").End(xlToRight).Column
For j = 3 To Colnumber
For i = 2 To 10
Set Condition = Worksheets("Sheet2").Cells(i, 1)
Sheet2.Cells(i, j).Value = Application.WorksheetFunction.SumIf(Range1, Condition)
Next i
Next j