Using LastColumn in a VBA Formula

stirlingmw1

Board Regular
Joined
Jun 17, 2016
Messages
53
Office Version
  1. 2016
  2. 2013
  3. 2010
  4. 2007
Platform
  1. Windows
Morning All

I am trying to add the formula "=Sum(from:to)" into a column within my worksheet. I can get the formula to work if I use column letters but i want to be able to add LastColumn into the formula.

the code I am using at present that works is:
VBA Code:
Sheets("Minor_22-23").Range(Cells(3, LastColumn), Cells(lastrow, LastColumn)).Formula = "=Sum(C" & ActiveCell.Row + 1 & ":Y" & ActiveCell.Row + 1 & ")"

what I want to do is use the formula =Sum(C" & ActiveCell.Row + 1 & , but where ":Y" is the column being used as the final column I want to use LastColumn & ActiveCell.Row + 1 & ")". but this final change doesnt work.
I have
VBA Code:
Dim LastColumn as Long 
LastColumn = Sheets("Minor_22-23").Cells(1, Sheets("Minor_22-23").Columns.Count).End(xlToLeft).Column

The reason I want the LastColumn referenced is that the formula could move left or right in the worksheet and I would like the formula to adjust also. What am i doing wrong?

Regards

Steve
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
VBA Code:
With Sheets("Minor_22-23")
   .Range(.Cells(3, LastColumn), .Cells(lastrow, LastColumn)).FormulaR1C1 = "=Sum(RC3:RC[-1])"
End With
 
Upvote 0
Solution
How about
VBA Code:
With Sheets("Minor_22-23")
   .Range(.Cells(3, LastColumn), .Cells(lastrow, LastColumn)).FormulaR1C1 = "=Sum(RC3:RC[-1])"
End With
Thank you, works great.

The speed of reply is appreciated.

Regards

Steve
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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