Runtime Error 91 Object variable or with block variable not set

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
On my userform, I have six option buttons and three textboxes. When I run this code I get the following runtime error '91.

Code:
Option Explicit


Dim sPath As String, sFile As String
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range


Private Sub cmdbtnOpen_Click()
    sPath = "C:\Production_Line\"
        
    Select Case True
    
        Case Is = optSlits
            sFile = sPath & "SDPF - LINE 1 .xlsx"
        Case Is = optMann
            sFile = sPath & "SDPF - LINE 2A.xlsx"
        Case Is = optCorb
            sFile = sPath & "SDPF - LINE 3.xlsx"
        Case Is = optIA
            sFile = sPath & "SDPF - LINE 4.xlsx"
        Case Is = optMann5
            sFile = sPath & "SDPF - LINE 5A.xlsx"
        Case Is = optPouch
            sFile = sPath & "SDPF - LINE 6.xlsx"
    End Select
    
    Select Case True
        Case Is = optSlits
            Worksheets("SDPF - LINE 1").Activate
        Case Is = optMann
            Worksheets("SDPF - LINE 2A").Activate
        Case Is = optCorb
            Worksheets("SDPF - LINE 3").Activate
        Case Is = optIA
            Worksheets("SDPF - LINE 4").Activate
        Case Is = optMann5
            Worksheets("SDPF - LINE 5A").Activate
        Case Is = optPouch
            Worksheets("SDPF - LINE 6").Activate
    End Select


    
    Set wb = Workbooks.Open(sFile)
    Unload Me
    
    Set rng = ws.Range("G:G").Find(Me.TextBox3.Value)

End Sub
The following line of code is highlighted in yellow:
Code:
Set rng = ws.Range("G:G").Find(Me.TextBox3.Value)
The error states that the object variable is not set, but it is. Why am I getting this error?

Thank You
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Option Explicit

Dim sPath As String, sFile As String
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range

Private Sub cmdbtnOpen_Click()

ws as Worksheet is shown above.
Thank you for the quick replay.
 
Last edited:
Upvote 0
The Dim statement only reserves space for the variable. You need to initialize it somewhere, like you initialize the wb variable. Something like:

Rich (BB code):
Set wb = Workbooks.Open(sFile)
    Unload Me

Set ws = wb.Worksheets("Sheet1")
    
    Set rng = ws.Range("G:G").Find(Me.TextBox3.Value)

Or

Set ws = Activesheet
 
Upvote 0
I just tried
Code:
Set rng = ActiveSheet.Range("G:G").Find(Me.TextBox4.Value)
and it worked. Thank You
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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