Subtotalling within a macro

Carly

Active Member
Joined
Aug 21, 2002
Messages
370
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?

Please help

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

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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
 
Upvote 0
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(y) = 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
 
Upvote 0

Forum statistics

Threads
1,213,565
Messages
6,114,338
Members
448,570
Latest member
rik81h

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top