Create a formula using VBA

swaink

Active Member
Joined
Feb 15, 2002
Messages
432
Hi All

Are you able to assist please

In my spreadsheet I have headers in row 4 from column I to Z

I am looking to put a sum of each column in row 4 and am looking to do this using vba

I have enabled my code to identify the last row to aid me with the formula but am struggling to get the formaula applied across all of the columns

The formulas will start in cell I4 but my formula below is not working

[I4].End(xlRight) = "=Sum(" & Range([I4], & lastrow) & ")"

Any advise is appreciated

Regards

Kevin
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi,

Try;

Code:
Range(Range("I4"), Cells(4, Columns.Count).End(xlToLeft)).Formula = "=SUM(" & Range("I4:I" & lastrow).Address & ")"
 
Upvote 0
Hi Mike

That work across the columns as expected

However the end result is that each column has the same formula =SUM($I$4:$I$249)

My expectation is that each column sum would refer to that specific column I.E
=SUM($I$4:$I$249),=SUM($J$4:$J$249),=SUM($K$4:$K$249) etc

Kev
 
Upvote 0
Sorry,

Code:
Range(Range("I4"), Cells(4, Columns.Count).End(xlToLeft)).Formula = "=SUM(" & Range("I4:I" & lastrow).Address(False, False) & ")"
 
Upvote 0
Thank you Mike

Thats fantastic it works great

I really appreciate the help this one had me stumped

Kev
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,820
Members
452,946
Latest member
JoseDavid

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