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
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
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
 

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,814
Office Version
  1. 2010
Platform
  1. Windows
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)"
 

Watch MrExcel Video

Forum statistics

Threads
1,123,057
Messages
5,599,550
Members
414,316
Latest member
ExcelLee

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
Top