RC Notation using a variable?

Rhino_Dance

Board Regular
Joined
Jul 23, 2008
Messages
74
The following vba line successfully inserts a Substitute Formula into one cell that refers to another cell 46 columns to the left:

ActiveCell.FormulaR1C1 = "=SUBSTITUTE(RC[-46],BoxPipe2,""|"")"

I want to replace the 46 with a numeric variable called 'nc'. But I get an error when I try to run the line which is shown below. The variable is definitely equal to 46. Can someone tell me why this doesn't work?

ActiveCell.FormulaR1C1 = "=SUBSTITUTE(RC[-nc],BoxPipe1,""|"")"


(Also I'm using SUBSTITUTE because the CELLS.REPLACE method does not always work for this case due to the imported characters I'm replacing.)

Thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Thanks. I tried that and got the message:

Compile error: Expected end of statement and the red part below was highlighted as the problem.

ActiveCell.FormulaR1C1 = "=SUBSTITUTE(RC[-"&nc&"],BoxPipe1,""|"")"

(Maybe it some how 'coupled' the first 2 quotation marks and couldn't make sense of the rest?)

Then I took the 2 quotes off and just had the &c2& but got the error:
'Application Defined or Object Defined Error'

Also tried ""&nc&"" which gave the same error as above.

BTW, my Dim statment is:

Dim nc As Integer

Which should be fine... right? Thanks again.
 
Upvote 0
Thanks. You're right about the nc. The BoxPipe1 needed no delimiters though. Formula is:


ActiveCell.FormulaR1C1 = "=SUBSTITUTE(RC[-" & nc & "],BoxPipe1,""|"")"

Thanks very much jindon- you really helped me out on this one!
 
Upvote 0
Hi All:

I would like to extend the above option to include the $ to fix whatever the column is calculated using nc. Lets say, it generates a formula like F42, I need $F42.

Any ideas?

Regards
DVR
 
Upvote 0
It seems there is an issue it simply take the cell and then puts the variable value in it. I gives a new formula, which is not intended.

DVR
 
Upvote 0
I am actually looking for a code where a cell is updated with a formula consisting of R & C details.
I mean the row number or colum number are caluclated based on other formula.
Finally, the vba code should give me something like R[variable]C[variable], where variable is calculated based on other formula. This I could get without any problems. However, I also need a dollar promt $ infront of column once the code runs.

That I could not get it. When I tried your earlier suggestion, it is simply keeping the variable value within the formula.

regards
DVR
 
Upvote 0

Forum statistics

Threads
1,223,532
Messages
6,172,878
Members
452,486
Latest member
standw01

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