How to create a formula without $ sign using VBA?

Rojo Habe

New Member
Joined
Nov 22, 2005
Messages
5
I need to sum a non-constant range and then autofill the resulting formula down the page. Finding the range is no problem; a small Do... While loop takes care of that for me, and gives me the range Cells(2,Start) to Cells(2,Finish).

The following line

.FormulaR1C1 = "=SUM(Weekly!R2C" & Start & ":R2C" & Finish & ")"

Places the formula into the spreadsheet as

=SUM(Weekly!$O$2:$R$2)

but I need it without the $ signs so that I can autofill it into the next 120 rows.

This is my second post today for something that will no doubt be incredibly simple once you know how! Excuse me for being thick but I'm still learning.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Not as easy as you might think.

Here is some code that fixes it "after the fact". The code I wrote takes the formula from the ActiveCell and removes all the "$" from it. Hopefully, you should be able to adapt it to work for you.
Code:
    Dim myFormula As String
    Dim myNewFormula As String
    myFormula = ActiveCell.Formula
    myNewFormula = Application.WorksheetFunction.Substitute(myFormula, "$", "")
    ActiveCell.Formula = myNewFormula
There may be a more efficient way of doing it, but it really depends on how the whole loop of your code works.
 
Upvote 0
Another way:

Code:
.FormulaR1C1 = Application.ConvertFormula("=SUM(Weekly!R2C" & start & ":R2C" & finish & ")", xlR1C1, , xlRelative)
 
Upvote 0
Andrew,

Not just "another way", but from the looks of it, a better way.
I did not know about "ConvertFormula". That is a good one to remember!
:biggrin:
 
Upvote 0
This is a great site! Every time I get stuck, someone always has the answer. Thanks, guys. I may even get this project finished today, at this rate.

Only one slight problem: Using Application.ConvertFormula seems to offset the row number by one. Where I used to get $O$2:$R$2 it now gives me O3:R3. Not a biggy, I can adjust this in the code, but I was just wondering what would cause this.
 
Upvote 0
The outcome depends on which cell contains the formula. You may need to add a RelativeTo argument:

Code:
.FormulaR1C1 = Application.ConvertFormula("=SUM(Weekly!R2C" & start & ":R2C" & finish & ")", xlR1C1, , xlRelative, .Cells(1, 1))
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,034
Members
448,940
Latest member
mdusw

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