Excel Macro: How to total a column with changing lenghth

mlbarry

New Member
Joined
Sep 26, 2012
Messages
47
How do I have the macro automatically calculate a column where the row number length changes: (Below is my macro)


Range("R2").Select
Selection.End(xlDown).Select
Range("R2333").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-2331]C:R[-1]C)"
Range("R2334").Select
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi mlbarry,

I see you have recorded this code, now it is time to learn how to edit it, and make it work better.

So, change the lines:

Selection.End(xlDown).Select
Range("R2333").Select

to lLastRow= Selection.End(xlDown).Row

And now you have a variable called lLastRow that you can use to make sure you have the last row used.

MIND YOU: This will not work if you have blanks in between your data. The data should be contiguous.

You also do not need to select an object to work with it MOST of the time.

Best of luck leaning some more about VBA.

Oh another tip: You could get better code if at times you change the mode of recording by toggling the Use Relative References. This button is below the record button on the Developer ribbon.
 
Last edited:
Upvote 0
Try
Code:
Range("R" & Rows.Count).End(xlUp).Offset(1).FormulaR1C1 = "=SUM(R1C:R[-1]C)"
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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