Using Lastcolumn in formula

coop123

Board Regular
Joined
Dec 18, 2018
Messages
66
Office Version
  1. 365
Good Morning

As part of a macro I am using I want to add a formula in M12 to sum all the values in every 3rd cell along row 12 from starting point O12 to last column. the last column will vary.

The formula that works is but this always has same range.
ActiveCell.Formula2R1C1 = _
"=SUMPRODUCT((MOD(COLUMN(RC[2]:RC[37])-COLUMN(RC[2]),3)=0)*1,RC[2]:RC[37])"

I have DIM LastColumn but don't know how to fit it into the above formula.
Dim LastColumn As Long
LastColumn = Cells(10, Columns.Count).End(xlToLeft).Column
I also need to copy the formula down to last row in column M.

Any help would be very much appreciated.

Thanks

Coop123
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I also need to copy the formula down to last row in column M.
Since M is the column the formula is going in, we most likely need a different column to determine the end of the data. I have assumed column O for that.

VBA Code:
Sub Insert_Formula()
  Dim LastColumn As Long
 
  LastColumn = Cells(10, Columns.Count).End(xlToLeft).Column
  Range("M12:M" & Range("O" & Rows.Count).End(xlUp).Row).Formula2R1C1 = _
    Replace("=SUMPRODUCT((MOD(COLUMN(RC[2]:RC#)-COLUMN(RC[2]),3)=0)*1,RC[2]:RC#)", "#", LastColumn)
End Sub
 
Upvote 0
Solution
Hi Peter

Your assumption was correct, it works perfectly.

I have also learnt a new way of copying the formula down without having to have more lines of code to move columns and fill down.

Thank you again.

coop123
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,712
Members
449,093
Latest member
Mnur

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