VBA Excel小计错误

时间:2016-06-24 13:24:52

标签: excel vba excel-vba

我收到了错误框:“Range类的小计方法失败”,代码如下。我该如何解决这个问题?如何重写代码以便它可以容纳不断变化的列数到小计?

    Range("A1").Select
Selection.Subtotal GroupBy:=3, Function:=xlSum, TotalList:=Array(14, 15, 16 _
    , 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, _
    43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63), Replace:= _
    True, PageBreaks:=False, SummaryBelowData:=True

1 个答案:

答案 0 :(得分:0)

如果它是一个数组,您可以像下面的

一样构建它
Dim ArrayNumbers() As String
Dim CounterArray As Long
For CounterArray = 1 To 49
ReDim Preserve ArrayNumbers(CounterArray)
'I'm not quite sure if parsing empty elements in the 'Total List' arg would give error, so instead we are going to start in the element 1 of the array doing the value 14 for it and so on.
ArrayNumbers(CounterArray) = IIf(CounterArray <> 1, "," & CounterArray+14, CounterArray+14) 'this is to avoid the comma in the first value just for all the other ones
Next CounterArray
Range("A1").Select
Selection.Subtotal GroupBy:=3, Function:=xlSum, TotalList:=ArrayNumbers, Replace:= _
    True, PageBreaks:=False, SummaryBelowData:=True
相关问题