Placing the same formula in every adjacent cell of used range

Human_doing

Board Regular
Joined
Feb 16, 2011
Messages
137
Hi all,

Can anyone please help with this. What I'm trying to do is use VBA to place a formula in every cell in Column B where there is an entry in the adjacent Column A. The number of used rows may vary every time. Could anyone please correct the below code? The required formula is
=COUNTIF($A$2:$A2,A2)>1


Code:
Dim Sh As Worksheet
Dim LR2 As Long
With Sh
LR2 = .Range("A" & .Rows.Count).End(xlDown).Row
      .Range("B2:B" & LR2).Formula = "=COUNTIF(R2C1:RC1,RC[-1])>1"
End With

The error I get is Run-time error 91: Object variable or With block variable not set.

Many thanks
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You haven't assigned a value to Sh You need something like

Set Sh=Sheets("Sheet1")
 
Upvote 0
Also, unless you want the formula right to the bottom of the whole worksheet, you'll need to change this
Rich (BB code):
LR2 = .Range("A" & .Rows.Count).End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,213,565
Messages
6,114,338
Members
448,570
Latest member
rik81h

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