"Object variable or With Block variable not set" error

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Can someone help me identify and resolve an "Object variable or With Block variable not set" error I am receiving with the code highlighted in red below. This is a combobox change event.

Rich (BB code):
Private Sub cb_r2_crew_change()
    Dim mbEvents As Boolean
    
    mbEvents = True
    
    Dim sel_crew1_start As String
    Dim sel_crew1_end As String
    Dim sel_crew2_start As Variant
    Dim sel_crew2_end As Variant
    Dim lrtime As Double
    Dim urtime As Double
    Dim sh_el As String
    
    sh_el = ""
    sel_crew1_start = WorksheetFunction.VLookup(Me.cb_r2_crew & "1", ws_vh.Range("R25:V42"), 4, False)
    sel_crew1_end = WorksheetFunction.VLookup(Me.cb_r2_crew & "1", ws_vh.Range("R25:V42"), 5, False)
    sel_crew2_start = Application.VLookup(Me.cb_r2_crew & "2", ws_vh.Range("R25:V42"), 4, False)
    If IsError(sel_crew2_start) Then
        sh_el = "X"
    Else
        sel_crew2_end = WorksheetFunction.VLookup(Me.cb_r2_crew & "2", ws_vh.Range("R25:V42"), 5, False)
        sh_el = WorksheetFunction.VLookup(Me.cb_r2_crew & "2", ws_vh.Range("R25:V42"), 2, False)
    End If
    lrtime = TimeValue(Me.tb_r2_sru)
    urtime = TimeValue(Me.tb_r2_srl)

    If WorksheetFunction.VLookup(Me.cb_r2_crew & "1", ws_vh.Range("R25:V42"), 2, False) = "X" And sh_el = "X" Then
        MsgBox "No staff scheduled on this crew"
        Exit Sub
    End If
    
    If lrtime > sel_crew1_end Then 'crew 1 can't
        MsgBox "This tournament service is scheduled for after this crew has left."
        Exit Sub
    ElseIf lrtime < sel_crew1_start Then 'crew 1 can't
        MsgBox "This tournament service is scheduled before this crew starts."
        Exit Sub
    End If
    
    If sh_el <> "X" Then 'only crew 1
        If lrtime > sel_crew2_end Then 'crew 1 can't
            MsgBox "This tournament service is scheduled for after this crew has left."
            Exit Sub
        ElseIf lrtime < sel_crew2_start Then 'crew 1 can't
            MsgBox "This tournament service is scheduled before this crew starts."
            Exit Sub
        End If
    End If
    mbEvents = False
End Sub

cb_r2_crew = "HPL"
"HPL1" exists in ws_vh, cell R30. The vlookup value (column 4) at U30 = 0.56
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
have you tried defining ws_vh?

Dim ws_vh as worksheet

Set ws_vh = activeworkbook.sheets("ws_vh")


Also giving what aspect you want from the combobox... either .Value or .Name or something like that.

Me.cb_r2_crew.Name & "1",
 
Last edited:
Upvote 0
Try using the Controls collection of the form.
Code:
Private Sub cb_r2_crew_change()
    Dim mbEvents As Boolean
    
    mbEvents = True
    
    Dim sel_crew1_start As String
    Dim sel_crew1_end As String
    Dim sel_crew2_start As Variant
    Dim sel_crew2_end As Variant
    Dim lrtime As Double
    Dim urtime As Double
    Dim sh_el As String
    
    sh_el = ""
    sel_crew1_start = Application.VLookup(Me.Controls("cb_r2_crew1").Value, ws_vh.Range("R25:V42"), 4, False)
    sel_crew1_end = Application.VLookup(Me.Controls("cb_r2_crew1").Value, ws_vh.Range("R25:V42"), 5, False)
    sel_crew2_start = Application.VLookup(Me.Controls("cb_r2_crew2").Value, ws_vh.Range("R25:V42"), 4, False)
    If IsError(sel_crew2_start) Then
        sh_el = "X"
    Else
        sel_crew2_end = Application.VLookup(Me.Controls("cb_r2_crew2").Value, ws_vh.Range("R25:V42"), 5, False)
        sh_el = Application.VLookup(Me.Controls("cb_r2_cre2").Value, ws_vh.Range("R25:V42"), 2, False)
    End If
    lrtime = TimeValue(Me.tb_r2_sru)
    urtime = TimeValue(Me.tb_r2_srl)

    If Application.VLookup(Me.Controls("cb_r2_crew1").Value, ws_vh.Range("R25:V42"), 2, False) = "X" And sh_el = "X" Then
        MsgBox "No staff scheduled on this crew"
        Exit Sub
    End If
    
    If lrtime > sel_crew1_end Then 'crew 1 can't
        MsgBox "This tournament service is scheduled for after this crew has left."
        Exit Sub
    ElseIf lrtime < sel_crew1_start Then 'crew 1 can't
        MsgBox "This tournament service is scheduled before this crew starts."
        Exit Sub
    End If
    
    If sh_el <> "X" Then 'only crew 1
        If lrtime > sel_crew2_end Then 'crew 1 can't
            MsgBox "This tournament service is scheduled for after this crew has left."
            Exit Sub
        ElseIf lrtime < sel_crew2_start Then 'crew 1 can't
            MsgBox "This tournament service is scheduled before this crew starts."
            Exit Sub
        End If
    End If
    mbEvents = False
End Sub
 
Last edited:
Upvote 0
Thank you both for your replies!!
The first solution was as tygrrboi had pointed out ... i hadn't defined and set the workbook. (Iguess ws_vh defined publicallyhas failed me somewhere.)

Norie ... thank you for enlightening me with some new knowledge. Your suggestion avoids the whole issue.
 
Upvote 0

Forum statistics

Threads
1,215,124
Messages
6,123,190
Members
449,090
Latest member
bes000

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