Passing Variables - "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
I have worksheet 'temp_ws' publicly defined in a module exclusive to defining public variables to be used with the scope of my entire project.

Rich (BB code):
public temp_ws as worksheet

temp_ws is set in the code of a standard module called "GetUniques", which is called from with some userform initialization code.
Rich (BB code):
Sub GetUniques(ByVal temp_ws As Object)

    'analyses data in schedule.csv determining range of dates within database and the number of records unique to those dates
    
    Dim d As Object             'scripting dictionary
    Dim c As Variant, i As Long
    Dim data_file_list As Name  'named range containing unique dates & record #s for use as listbox rowsource
    
    Application.ScreenUpdating = False
    'MsgBox wb_sched.Name
    With ws_sched
        Set d = CreateObject("Scripting.Dictionary")
        'llastrow = .Cells(Rows.Count, 1).End(xlUp).Row
        c = .Range("M1:M" & llastrow)
        For i = 1 To UBound(c, 1)
            d(c(i, 1)) = 1
        Next i
    End With

    Set temp_ws = wb_sched.Worksheets.Add
    temp_ws.Name = "temp_ws"

Once this module has been processed, the application resumes with the userform initialization code. Upon reaching this line ...
Rich (BB code):
Refine_Schedule temp_ws
Code within module 'Refine_Schedule' is executed ...

Rich (BB code):
Sub Refine_Schedule(ByVal temp_ws As Object)
    
    Dim c1 As Long, norec As Long
    Dim t_fac As String, myVal As Variant, bv1 As String, bv2 As String
    Dim red_ref As Long, red_fcn As Long, red_fac As Long, red_class As Long, red_classc As Long
    Application.ScreenUpdating = False

    
    With ws_sched
        ,,, code ,,,
    End With
        
    red_ref = Application.WorksheetFunction.CountIf(ws_core.Range("A:A"), "ref")          'count of reference eliminations
    red_fcn = Application.WorksheetFunction.CountIf(ws_core.Range("A:A"), "fnc")          'count of function eliminations
    red_fac = Application.WorksheetFunction.CountIf(ws_core.Range("A:A"), "fac")         'count of facility eliminations
    red_class = Application.WorksheetFunction.CountIf(ws_core.Range("A:A"), "X")        'redundant facilities eliminations
    red_classc = Application.WorksheetFunction.CountIf(ws_core.Range("A:A"), "X2")         'no service class C eliminations
             
    Debug.Print temp_ws.Name
             
    With temp_ws
        ,,, code ,,,
    End With
    
 End Sub

I am receiving an "Object variable or With block variable not set" error with the lines in red. It appears that the value for 'temp_ws' wasn't carried over despite it being publicly defined and set in previous code. One of my biggest challenges is passing shared variables between modules and between userforms and modules. Clearly this is once again the issue.

Will anyone advise where I may have went wrong in my code and/or assumptions?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You will probably have to step through the code line by line to see where it variable loses its value. I don't see any problem with code posted, but that doesn't mean anything. You could try changing your declaration in Refine_Schedule from temp_ws As Object to temp_ws As Worksheet to be consistent with the public variable declaration.
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

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