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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi all,

I have a similar problem and I am not sure how to solve it. I would like to use the number of a certain row (changing, depending of length of the file) to use a reference to go to in another sheet. So basically I want to go to the same row in another sheet but don't know which row that is.

Assume that I have found the starting cell in row 21 on my first sheet and B21 is selected.

Dim var_row As Integer var_row = "=ROW()"
Rows(ActiveCell.Row).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Cells(1, var_row).Select

But already the second line returns Run-time error '13': Type mismatch

Could anyone point out what I need to change in order to make this work? Much appreciated :)
 
Upvote 0
try

SUBSTITUTE(RC[-" & nc & "]


1) remember that the "&" needs a space before it as vba can confuse the notation as a data type.

2) I am also used to passing scripts across unix systems in my other work so double quotes always makes me nervous so when i need a quote I will do something like ..sdsf" & chr(34) & variable & chr(34) & '....rest of the text.
 
Upvote 0
However, I also need a dollar promt $ infront of column once the code runs.

Remove the [] brackets to get $ in the reference.

R1C1 = Absolute = $A$1
R[1]C[1] = Relative = A1
R1C[1] = Row Absolute, Col Relative = $A1
R[1]C1 = Row Relative, Col Absolute = A$1

The difference with absolute vs relative.
Absolute, you need your variable to be the actual column #, A=1 B=2 C=3 etc
Relative, you need your variable to be a number 'away from' the cell the formula is entered in.
 
Upvote 0
Also, just FYI.
It's possible and quite easy to reference cells with column #s
So you can use .Formula instead of .FormulaR1C1

Cells(Row#,Column#).Address(RowAbsolute,ColAbsolute)

Cells(1,1).Address(0,0) will produce A1
Cells(5,7).Address(1,0) will produce G$5
Cells(23,2).Address(0,1) will produce $B23
Cells(5,5).Address(1,1) will produce $E$5
 
Upvote 0

Forum statistics

Threads
1,223,444
Messages
6,172,171
Members
452,445
Latest member
walkman99

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