VB code adjustment to remove fixed references

thomasbeard

Board Regular
Joined
Oct 19, 2005
Messages
139
I have the following VB code that will create a sum formula that will add everything directly to the left:

Activecell.FormulaR1C1 = "=SUM(RC[-1]:" & ActiveCell.End(xlToLeft).Address(ReferenceStyle:=xlR1C1) & ")"

However it comes out in the format:

SUM($A1:E$1)

whereas it needs to be in the format

SUM(A1:E1)

Can someone please help me fix this? Thanks

Tom
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
If you add ,RowAbsolute:= False, ColumnAbsolute:=False after the reference style bit, you'll lose your $s. Unfortunately, your formula will also stop working (but only because it isn't really working at the moment - if it were, both $s would be on the same cell reference).

Try:
ActiveCell.FormulaR1C1 = "=SUM(RC[" & ActiveCell.Offset(0, -1).End(xlToLeft).Column - ActiveCell.Column & "]:RC[-1])"
 
Upvote 0
You can also use:
Code:
 Activecell.FormulaR1C1 = "=SUM(" & activecell.Offset(0,-1).End(xlToLeft).Address(false, _
false, xlR1C1,false, activecell) & ":RC[-1])"
FWIW.
 
Upvote 0
Another shot:
Code:
Sub Test()
Dim strMyFormula As String
Dim rng As Range

Set rng = Range(ActiveCell.Offset(0, -1), ActiveCell.End(xlToLeft))
strMyFormula = "=SUM(" & rng.Address(RowAbsolute:=False, ColumnAbsolute:=False) & ")"
Debug.Print strMyFormula

ActiveCell.Formula = strMyFormula

End Sub
 
Upvote 0
When using R1C1 reference style, the RelativeTo argument needs to be supplied for all relative addresses.
Code:
Activecell.FormulaR1C1 = "=SUM(RC[-1]:" & ActiveCell.End(xlToLeft).Address(False,False,xlR1C1,False,ActiveCell) & ")"
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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