0

我需要你的帮助。

如果我正在运行此代码,则不会出现问题(请注意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(cells(2,j+3), cells(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

4

1 回答 1

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
于 2013-03-27T21:27:51.593 回答