在excel中引用数组的一部分

我有一个数组,需要引用该数组中的单个列,以便我可以在计算中使用它。 有没有一种方法,我可以直接做,如下所示的sumifs函数的占位符的占位符,或者我需要通过循环手动执行sumif?

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

对于这样的事情,我更喜欢使用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 

我会build议Indexfunction。 它需要范围,所以ScoresArray和参数必须是范围。 可能的情况(testing):

 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 

我希望这有帮助!

您可以参考范围内的范围,这可能有点令人困惑,但是如果您引用范围(“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。 你忘了设置variables赋值。 没有它,你分配范围的值,而不是范围本身。