我有一个数组,需要引用该数组中的单个列,以便我可以在计算中使用它。有没有办法我可以直接执行它,如下面的sumifs函数中的参数的占位符所示,或者我是否需要通过循环手动执行sumif?
Sub testarrayinxlformulas()
ScoresArray = Sheets("Scores").Range("A1").CurrentRegion
ScoresSum1 = Application.WorksheetFunction.Sumifs(5thColumnofArrayGoesHere,4thColumnHere,"Good",3rdColHere,VariableGoesHere)
End Sub
答案 0 :(得分: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
答案 1 :(得分: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
我希望这有帮助!
答案 2 :(得分:0)
你可以引用范围内的范围,这可能有点令人困惑,但是如果你引用像这个范围(“B:C”)这样的范围。范围(“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)
NB。你忘了在变量赋值上设置。没有它,您可以指定范围的值,而不是范围本身。