How to use relative reference in a formula using vba

hutch27

New Member
Joined
May 5, 2014
Messages
37
Hello -

I'm using an If-Then function in vba to insert a formula into specific cells based on the function. However, I'm having trouble with vba using an absolute rather than relative reference.

Here is my code:

Sub Macro12()
Dim i As Long
For i = 1 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
If ActiveSheet.Cells(i, 3) = "Lookup Value" Then
ActiveSheet.Range("H" & i).Formula = "=Table_Cash[@[Amount]]-G32"
End If
Next i
End Sub

If you notice in the formula, "=Table_Cash[@[Amount]]-G32" is a value from a table (this table is variable) and as such, uses relative reference - which i'm fine with. Alternatively, "G32" is an absolute reference not in a table, and, as such, uses absolute reference. How can i make it such that G32 is a relative reference?

Also - How would i add a subtotal to the code?
 
Last edited:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Assuming table's headers in row 1, maybe this...

Code:
Sub Macro12()
Dim i As Long
For i = [COLOR=#ff0000]2[/COLOR] To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    If UCase(ActiveSheet.Cells(i, 3)) = "LOOKUP VALUE" Then
        ActiveSheet.Range("H" & i).Formula = "=Table_Cash[@[Amount]]-G" & [COLOR=#ff0000]30 + i[/COLOR]
    End If
Next i
End Sub

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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