Edit formula in cell w/ dynamic column reference

boothrat

New Member
Joined
Feb 4, 2014
Messages
13
I'm trying to write a macro that will add to the formula already in an active cell. I'm having difficulty getting it to change the column that the formula references. The column always needs to be the one that the cell I'm working with is in, even the column reference to the other sheets. This cell won't always be added to the same row but will always reference the same rows.


Code:
Sub Data_Add()

Dim Contents As String


Contents = ActiveCell.Formula
ActiveCell.Formula = Contents & "+((Totals!N$33+Totals!N$34+Totals!N$35)*Data!N$45*0.6)"




End Sub

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi

Use the R1C1 notation, like:

Code:
Sub Data_Add()
Dim Contents As String

Contents = ActiveCell.FormulaR1C1
ActiveCell.FormulaR1C1 = Contents & "+((Totals!R33C+Totals!R34C+Totals!R35C)*Data!R45C*0.6)"

End Sub
 
Upvote 0
Do you mean you want the N in N$33 to be the same column as the activecell? Try this:

Code:
Dim Contents As String
Dim ACcolumn As Integer


ACcolumn = ActiveCell.Column
Contents = ActiveCell.Formula

ActiveCell.Formula = Contents & "+((Totals!" & Cells(33, ACcolumn).Address & "+Totals!" _
& Cells(34, ACcolumn).Address & "+Totals!" & Cells(35, ACcolumn).Address _
& ")*Data!" & Cells(45, ACcolumn).Address & "*0.6)"
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

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