How to use a variable in R1C1

rassten

Active Member
Joined
Aug 31, 2008
Messages
310
This does not work:
Code:
LR = Cells(Rows.Count, 1).End(xlUp).Row
'Range("B2").FormulaR1C1 = "=COUNTIF(R2C1:R&"LR"&C1,RC[-1])"
Range("B2").Formula = "=COUNTIF($A$2:$A$LR, A2)"
    Selection.AutoFill Destination:=Range("B2:B25")
    Range("B2:B25").Select
But how do use a variable in VBA, R1C1 style
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi Sten

You had the ampersand and quotes in the wrong order:

Code:
'Range("B2").FormulaR1C1 = "=COUNTIF(R2C1:R" & LR & "C1,RC[-1])"
 
Upvote 0
Notice that you don't need the autofill, you can apply the formula directly to the whole range.
 
Upvote 0
Thank you Richard, that was a big help.

For me R1C1-style is hard to understand.

doofusboy, yes, but I but at some point I have to learn R1C1.

pgc01, How?
 
Upvote 0
Hi Sten

Try:

Code:
Sub Test()
Dim LR As Long
 
LR = Cells(Rows.Count, 1).End(xlUp).Row
Range("B2:B" & LR).FormulaR1C1 = "=COUNTIF(R2C1:R" & LR & "C1,RC[-1])"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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