VBA .Formula using Cells(r,c) to identify ranges creates absolute references - How to create relative instead

psulion01

Board Regular
Joined
Sep 25, 2002
Messages
127
Hopefully this is a simple one...

Code:
Cells(LastRow + 1, c).Formula = "=SUM(" & Cells(7, c).Address & ":" & Cells(LastRow, c).Address & ")"

That line of code produces a formula in the target cell along the lines of "=SUM($A$7:$A$10)"

The problem is that later in my code I run a horizontal sort and afterwards all of the formulas are wrong because the absolute column references don't change with the sort (i.e. column A is now Column C, yet the formulas in Column C are all still "=SUM($A$7:$A$10)"

Is there a way around this using my existing code, or do I need to utilize R1C1 or something like that? If so can someone assist with the line above?

Thanks!
Mike
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Code:
.formulaR1C1

Basically your formula will look like "=sum(R7C" & c & ":R" & lastrow & "C" & c & ")"
 
Upvote 0
Thanks... no other way to use the Cells(r,c) method? R1C1 always seems so hard to read...

Appreciate the fast response!

edit:
Just tried it and it seems like the same problem persists. Calcs are right until the sort, then things are all out of place. Can I somehow force relative references for the columns?
 
Last edited:
Upvote 0
[SOLVED] Re: VBA .Formula using Cells(r,c) to identify ranges creates absolute references - How to create relative instead

Found it... very simple.

Code:
Cells(LastRow + 1, c).Formula = "=SUM(" & Cells(7, c).Address(False, False) & ":" & Cells(LastRow, c).Address(False, False) & ")"
 
Upvote 0
Re: [SOLVED] Re: VBA .Formula using Cells(r,c) to identify ranges creates absolute references - How to create relative instead

We both learned something then :p
 
Upvote 0
This is one of those cases that begs for an R1 formula:

Code:
Cells(LastRow + 1, c).FormulaR1C1 = "=SUM(R7C:R[-1]C)"
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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