2016-06-24 20 views
1

以下のコードを使用して、 "Rangeクラスの小計メソッドが失敗しました"というエラーボックスが表示されました。これを修正するにはどうしたらいいですか?コードを書き換えて、小計に変更する列数に対応できるようにするにはどうすればよいですか?VBA Excel小計エラー

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 
+0

ないように、このヘルプにそれを構築することがあります。http://stackoverflow.com/questions/23276881/run-time-error-1004-subtotalを-modod-of-range-class-failed – Vityata

+1

*変更する列の数に対応できるようにコードを書き直すにはどうすればいいですか? - 各列をループして列番号を配列変数にロードする必要があります。これを 'TotalList'引数に渡します。 –

答えて

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 
+0

「Selection.Subtotal」行で「ランタイムエラー1004 Rangeクラスの小計メソッドが失敗しました」。また、列番号を自分で入力したい場合は、「49」を「i」に変更できますか? – lindqmx1

+0

ええ、あなたはそのコードで何を達成しようとしていますか? – Sgdva

+0

C列のデータが変わるたびに、可変量の列に対して行小計を追加する必要があります。私はcounterArrayに変数を追加する方法を知っていますが、コードは上記のエラーを受け取ります。 – lindqmx1