1

我有一个数组,需要引用该数组中的单个列,以便我可以在计算中使用它。有没有一种方法可以直接按照下面 sumifs 函数中参数的占位符指示进行操作,还是需要通过循环手动执行 sumif?

Sub testarrayinxlformulas()
ScoresArray = Sheets("Scores").Range("A1").CurrentRegion
ScoresSum1 = Application.WorksheetFunction.Sumifs(5thColumnofArrayGoesHere,4thColumnHere,"Good",3rdColHere,VariableGoesHere)
End Sub
4

3 回答 3

1

对于这样的事情,我更喜欢使用 With 语句,然后使用 .Columns 属性,如下所示:

With Sheets("Scores").Range("A1").CurrentRegion
    ScoresSum1 = WorksheetFunction.SumIfs(.Columns(5), .Columns(4), "Good", .Columns(3), myVariable)
End With

或扩展可读性:

With Sheets("Scores").Range("A1").CurrentRegion
    ScoresSum1 = WorksheetFunction.SumIfs(.Columns(5), _
                                          .Columns(4), "Good", _
                                          .Columns(3), myVariable)
End With
于 2013-09-12T15:26:48.573 回答
0

我会建议这个Index功能。它需要范围,因此ScoresArray参数必须是范围。一个可能的场景(经过测试):

Sub testarrayinxlformulas()
    Dim ScoresArray As Range, ScoresSum1 As Double
    Dim Scores5th As Range, Scores4th As Range, Scores3rd    As Range, VariableGoesHere As Range
    Set ScoresArray = Sheets("Scores").Range("A1").CurrentRegion
    Set Scores5th = WorksheetFunction.Index(ScoresArray, 0, 5)
    Set Scores4th = WorksheetFunction.Index(ScoresArray, 0, 4)
    Set Scores3rd = WorksheetFunction.Index(ScoresArray, 0, 3)
    ScoresSum1 = Application.WorksheetFunction.SumIfs(Scores5th, Scores4th, "Good")
End Sub

我希望这有帮助!

于 2013-09-12T10:37:40.523 回答
0

您可以引用范围内的范围,这可能有点令人困惑,但如果您引用这样的范围 Range("B:C").Range("A:A") 将返回列 B。

因此,您可以这样编写代码:

Set ScoresArray = Sheet1.Range("A1").CurrentRegion
ScoresSum1 = Application.WorksheetFunction.SumIfs(
    ScoresArray.Range("E:E"),
    ScoresArray.Range("D:D"), "Good", 
    ScoresArray.Range("C:C"), VariableGoesHere)

注意。您忘记了变量赋值上的 Set 。没有它,您分配范围的值而不是范围本身。

于 2013-09-12T10:40:21.553 回答