Error when trying to set up variable range

Human_doing

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

Can anyone please correct the below code which gives error: 'Run-time error '91': Object variable or With block variable not set. What the code is supposed to do is ask user to open an Excel file, count how many rows of data there are (which will vary each time) and insert the below formula in to each cell in column H for rows with data.

Code:
Private Sub CommandButton1_Click()
'Ask user to select Excel file
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select the file with raw data for the report")
If NewFN = False Then
' They pressed Cancel
MsgBox "Stopping because you did not select a file"
Exit Sub
Else
Workbooks.Open Filename:=NewFN
End If
'Set range for duplicate count
Dim Sh As Worksheet
Dim LR As Long
With Sh
LR = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("I2:I" & LR).Formula = "COUNTIF(H:H,H2)>1"
End With
 
End Sub

Thanks
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Change to this:
Code:
If NewFN = [B][COLOR="red"]"[/COLOR][/B]False[B][COLOR="Red"]"[/COLOR][/B]
 
Upvote 0
Rich (BB code):
Private Sub CommandButton1_Click()
'Ask user to select Excel file
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select the file with raw data for the report")
If NewFN = False Then
' They pressed Cancel
MsgBox "Stopping because you did not select a file"
Exit Sub
Else
Workbooks.Open Filename:=NewFN
End If
'Set range for duplicate count
Dim Sh As Worksheet
Dim LR As Long
With Sh
LR = .Range("A" & .Rows.Count).End(xlUp).Row
End With
 
End Sub
Line's I've highlighted in red - where do you define what sheet you want set to the variable SH?
 
Upvote 0
Hi, thanks for getting back to me so quickly. I've used this code before for when doing loops through numerous worksheets, maybe on this occasion as there is only one worksheet I could take out some of the code i.e. remove the red lines?
 
Upvote 0
If you take out the lines in red then your variable LR won't be assigned a value and will fail.

This may work, I'm making the assumption that when you open the workbook, the first sheet that is active is the sheet you want LR based on:

Rich (BB code):
Private Sub CommandButton1_Click()

'Ask user to select Excel file
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select the file with raw data for the report")

If NewFN = False Then
    ' They pressed Cancel
    MsgBox "Stopping because you did not select a file"
    Exit Sub
Else
    Workbooks.Open Filename:=NewFN
End If
 
 
'Set range for duplicate count
Dim Sh As Worksheet
Dim LR As Long
Set Sh = ActiveSheet
With Sh
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
End With
 
End Sub
Changed part in red vs your original posting
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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