# Subtotalling within a macro

#### Carly

##### Active Member
I am trying to subtotal a spreadsheet within a macro but I am unsure how many columns there will be each time.
When you record the subtotalling it puts in the column numbers (TotalList:=Array(5,6,etc) which is no good as one day there might be 5 and another day there might be 50.

Can this be done by somehow naming a range like in a Pivot Table?

Carly
This message was edited by Carly on 2002-10-18 04:19

### Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

#### Yogi Anand

##### MrExcel MVP
Hi Carly:

I think if you use a Named Range, as you suggested in your post, it should work -- you could set it up to be a dynamic range so the extent of the range is taken care of when the range expands or shrinks.

Regards!

Yogi

#### Ekim

##### Well-known Member
This post appears similar to a post in another NG. This is the macro from that other NG ( author not recorded).

<pre>
Sub Test()

Dim Rng As Range
Dim x As Integer
Dim y As Integer
Dim Arr() As Integer

' *** Change starting cell in table to suit ***
Set Rng = Range("A1").CurrentRegion
' *** Change starting column to suit

y = 1
For x = 2 To Rng.Columns.Count
ReDim Preserve Arr(1 To y)
Arr = x
y = y + 1
Next x
Rng.Subtotal groupBy:=1, Function:=xlSum, totalList:=Arr

End Sub
</pre>

Notes:
1. The macro will cater for varying number of columns.
2. See the line -

For x = 2 To Rng.Columns.Count

This controls what columns to start subtotaling from. In this case column 2 (i.e. column B). If you want to start subtotaling from column E, change the line to

For x = 5 To Rng.Columns.Count

HTH

Mike

Replies
2
Views
256
Replies
15
Views
830
Replies
1
Views
526
Replies
2
Views
234
Replies
42
Views
3K

1,181,096
Messages
5,928,048
Members
436,586
Latest member
latintxn

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back