+ Reply to Thread
Results 1 to 9 of 9

Can't pass arguments to a declared class object: obj variable or with block not set

Hybrid View

excelicus Can't pass arguments to a... 08-19-2018, 07:29 PM
mikerickson Re: Can't pass arguments to a... 08-19-2018, 07:59 PM
excelicus Re: Can't pass arguments to a... 08-19-2018, 10:51 PM
Kyle123 Re: Can't pass arguments to a... 08-20-2018, 03:55 AM
Kyle123 Re: Can't pass arguments to a... 08-20-2018, 04:02 AM
excelicus Re: Can't pass arguments to a... 08-20-2018, 09:32 AM
Kyle123 Re: Can't pass arguments to a... 08-20-2018, 11:39 AM
Kyle123 Re: Can't pass arguments to a... 08-20-2018, 11:46 AM
excelicus Re: Can't pass arguments to a... 08-20-2018, 01:18 PM
  1. #1
    Registered User
    Join Date
    12-20-2014
    Location
    North America
    MS-Off Ver
    2013
    Posts
    40

    Can't pass arguments to a declared class object: obj variable or with block not set

    I declared a sub in the Class module 'vehRows', to allow me to pass arguments, similar to a class constructor in Java or C++
    Public Sub InitializeAttributes(sourceVeh_c As String, vehNum_c As String, impFilePrefix_c As String, expFilePrefix_c As String, _
                                 pitGroupPrefix_c As String, pitGroupSuffix_c As String, driver_c As String, descriptionPrefix_c As String, _
                                 classes_c As String, category_c As String, importSheet_ As Worksheet, descriptionDelimeter_c As String)
    This is the full Class Module
       
    
    Public importSheet As Worksheet
    
    Public sourceVeh As String
    
    Public expFilePrefixStr As String
    
    Private pitGroupPrefix As String
    Private impFilePrefix As String
    Private pitGroupSuffix As String
    Private descriptionPrefix As String
    Private descriptionDelimeter As String
    
    Private defaultLiveryStr As String
    Private vehNumStr As String
    Private pitGroupStr As String
    Private driverStr As String
    Private descriptionStr As String
    Private classesStr As String
    Private categoryStr As String
    
    '+--------------+-------+--------------------+--------------------+-----------------+-----------------+--------+--------------------+---------+----------+
    '| Source veh # | Veh # | Import file Prefix | Export file Prefix | PitGroup Prefix | PitGroup Suffix | Driver | Description Prefix | Classes | Category |
    '+--------------+-------+--------------------+--------------------+-----------------+-----------------+--------+--------------------+---------+----------+
    '|            1 |     2 |                  3 |                  4 |               5 |               6 |      7 |                  8 |       9 |       10 |
    '+--------------+-------+--------------------+--------------------+-----------------+-----------------+--------+--------------------+---------+----------+
        
    '+-----------------------+
    '| Attributes of vehFile |
    '+-----------------------+
    '| DefaultLivery=        |
    '| Number=               |
    '| PitGroup=             |
    '| Driver=               |
    '| Description=          |
    '| Classes=              |
    '| Category=             |
    '+-----------------------+
    
    'constructor
    Private Sub Class_Initialize()
        'This sub seems to be pointless
        
        'Me.* is not to be used here
        defaultLivery(expFilePrefixStr) = vehNum
        pitGroup(pitGroupPrefix) = pitGroupSuffix
        description(descriptionPrefix, descriptionDelimeter) = vehNum
    End Sub
    
    
    
    Public Sub InitializeAttributes(sourceVeh_c As String, vehNum_c As String, impFilePrefix_c As String, expFilePrefix_c As String, _
                                 pitGroupPrefix_c As String, pitGroupSuffix_c As String, driver_c As String, descriptionPrefix_c As String, _
                                 classes_c As String, category_c As String, importSheet_ As Worksheet, descriptionDelimeter_c As String)
        
        Set importSheet = importSheet_c
        
        sourceVeh = sourceVeh_c
        impFilePrefix = impFilePrefix_c
        expFilePrefixStr = expFilePrefix_c
        pitGroupPrefix = pitGroupPrefix_c
        pitGroupSuffix = pitGroupSuffix_c
        
        descriptionPrefix = descriptionPrefix_c
        descriptionDelimeter = descriptionDelimeter_c
        
        'None of the following come up in Me.* intellisence suggestion
        'Me.* is not to be used here
        vehNum = vehNum_c
        driver = driver_c
        classes = classes_c
        category = category_c
        
        
        'It made more sense to place thse in the Class_Initialize() sub, since they format already initialized variables.
    
        
        
        
        'Porential ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        'Me.importSheet = importSheet
    End Sub
    
    '+-----------------------+
    '| Attributes of vehFile |
    '+-----------------------+
    '| DefaultLivery=        |
    '| Number=               |
    '| PitGroup=             |
    '| Driver=               |
    '| Description=          |
    '| Classes=              |
    '| Category=             |
    '+-----------------------+
    Private Property Let defaultLivery(expFilePrefix_m As String, vehNum_m As String)
        defaultLiveryStr = """" & expFilePrefixStr & vehNum & ".DDS"""
    End Property
    Public Property Get defaultLivery(expFilePrefix_m As String) As String
        Let defaultLivery = defaultLiveryStr
    End Property
    
    Private Property Let vehNum(vehNum_m As String)
        vehNum = vehNum_m
    End Property
    Public Property Get vehNum() As String
        Let vehNum = vehNumStr
    End Property
    
    Private Property Let pitGroup(pitGroupPrefix As String, pitGroupSuffix As String)
        pitGroupStr = pitGroupPrefix + pitGroupSuffix
        
        If (pitGroupStr = "") Then
            pitGroupStr = importSheet.Cells.Find(What:="PitGroup=", LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
        End If
        
        'Let Me.pitGroup = pitGroupStr
    End Property
    Public Property Get pitGroup(pitGroupPrefix As String) As String
        Let pitGroup = pitGroupStr
    End Property
    
    Private Property Let driver(driver_m As String)
        Let driverStr = driver_m
        
        If (driverStr = "") Then
            driverStr = importSheet.Cells.Find(What:="Driver=", LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
        End If
        
        Let Me.pitGroup = pitGroupStr
    End Property
    Public Property Get driver() As String
        Let driver = driverStr
    End Property
    
    Private Property Let description(descriptionPrefix_c As String, descriptionDelimeter_c As String, vehNum_c As String)
        
        If (descriptionPrefix_c = "") Then
            descriptionPrefix_c = importSheet.Cells.Find(What:="Description=", LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
            'descriptionPrefix_c
            'Check if delimiter exists
            
            If InStr(descriptionPrefix_c, descriptionDelimeter_c) <> 0 Then 'extract up to delimiter so we can later assign unique numbers to it
               descriptionPrefix_c = Mid(descriptionPrefix_c, 2, InStr(descriptionPrefix_c, descriptionDelimeter_c) - 2) 'Start after ", hence 2
            Else 'Start from the right, & try to spot numeric number if possible
                Dim isInteger As Boolean
                isInteger = False
                Dim loopCounter As Integer
                Dim booleanTrueCounter As Integer         'If booolean changes back to false, then terminate loop
                
                loopCounter = 0
                booleanTrueCounter = 0
                
                Dim descriptionPrefix_MaxLen As Integer
                
                For i = (Len(descriptionPrefix_c) - 1) To 1 Step -1        'Work backwards, before last ", hence Len()-1
                    isInteger = IsNumeric(Mid(descriptionPrefix_c, i, 1))         'Might cause LOGICAL ERROR !!!!!!!!!!!!!!!!!!!!!!!
                    loopCounter = loopCounter + 1
                    
                    If isInteger = True Then
                        booleanTrueCounter = booleanTrueCounter + 1
                    ElseIf isInteger = False And booleanChangeCounter > 0 Then
                        descriptionPrefix_MaxLen = loopCounter      'may be able to substitute loopCounter with "(Len(descriptionPrefix_c) - 1) - 1"
                        descriptionPrefix_c = Left(descriptionPrefix_c, Len(descriptionPrefix_c) - loopCounter)
                        Exit For
    '                ElseIf isInteger = False And i = 1 Then
    '                    'No numeric value was found, leave as as is
    '                    descriptionPrefix_c = Mid(descriptionPrefix_c, 2, InStr(descriptionPrefix_c, descriptionDelimeter_c) - 2)
                    End If
                Next i
            End If
        End If
        
        descriptionStr = descriptionPrefix_c + descriptionDelimeter_c + vehNum
        
        'Let description = descriptionStr
    End Property
    Public Property Get description(descriptionPrefix_c As String, descriptionDelimeter_c As String) As String
        Let description = descriptionStr
    End Property
    
    Private Property Let classes(classes_m As String)
        classesStr = classes_m
        
        If (classesStr = "") Then
            classesStr = importSheet.Cells.Find(What:="Classes=", LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
        End If
    End Property
    Public Property Get classes() As String
        Let classes = classesStr
    End Property
    
    Private Property Let category(category_m As String)
        If (category_m = "") Then
            category_m = importSheet.Cells.Find(What:="Category=", LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
        End If
        
        categoryStr = category_m
    End Property
    Public Property Get category() As String
        Let category = categoryStr
    End Property
    
    '+-----------------------+
    '| Attributes of vehFile |
    '+-----------------------+
    '| DefaultLivery=        |
    '| Number=               |
    '| PitGroup=             |
    '| Driver=               |
    '| Description=          |
    '| Classes=              |
    '| Category=             |
    '+-----------------------+
    In the main method/class/function, I have the following statement to pass the information to the class instance, vehRow8

            Dim vehRows8 As New vehRows
    
            vehRows8.InitializeAttributes sourceVeh, vehNum, impFilePrefix, expFilePrefix, _
                                        pitGroupPrefix, pitGroupSuffix, driver, descriptionPrefix, _
                                        classes, category, importSheet, descriptionDelimeter
    Full error message is,

    Run-time error '91':
    Object variable or With block variable not set
    Attached Files Attached Files

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Can't pass arguments to a declared class object: obj variable or with block not set

    This sounds like a duplicate of your other thread. https://www.excelforum.com/excel-pro...onsistent.html
    Which you have marked as solved.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    12-20-2014
    Location
    North America
    MS-Off Ver
    2013
    Posts
    40

    Re: Can't pass arguments to a declared class object: obj variable or with block not set

    The error message is different & takes place in a different class.

    I mislabeled a variable, importSheet_c as importSheet in the class module.

    The same error still persists.

    If I move the following from Class_Initialize() to InitializeAttributes
        'It made more sense to place tehse in the Class_Initialize() sub, since they format already initialized variables.
        'Me.* is not to be used here
        defaultLivery(expFilePrefixStr) = vehNum
        pitGroup(pitGroupPrefix) = pitGroupSuffix
        description(descriptionPrefix, descriptionDelimeter) = vehNum
    The error is displayed: Run time error 28, out of stack space
    Last edited by excelicus; 08-19-2018 at 11:27 PM.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Can't pass arguments to a declared class object: obj variable or with block not set

    I'll have a look

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Can't pass arguments to a declared class object: obj variable or with block not set

    It's because at this point:
    Private Sub Class_Initialize()
        'This sub seems to be pointless
        
        'Me.* is not to be used here
        'defaultLivery(expFilePrefixStr) = vehNum
        pitGroup(pitGroupPrefix) = pitGroupSuffix
        'description(descriptionPrefix, descriptionDelimeter) = vehNum
    End Sub
    Where you set the pitGroup property, the importSheet isn't set (therefore you're trying to call find on an a worksheet you haven't set), you're only setting this in the InitializeAttributes Sub after the property is called:
    Private Property Let pitGroup(pitGroupPrefix As String, pitGroupSuffix As String)
        pitGroupStr = pitGroupPrefix + pitGroupSuffix
        
        If (pitGroupStr = "") Then
            pitGroupStr = importSheet.Cells.Find(What:="PitGroup=", LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
        End If
        
        'Let Me.pitGroup = pitGroupStr
    End Property
    It'll be the same for description if you comment out that line. All this init code, needs to come after setting the properties.

    As an aside, don't do this:
    Dim vehRows8 As New vehRows
    It'll cause issues that you may find hard to debug, be explicit:
    Dim vehRows8 as vehRows
    Set vehRows8 = New vehRows

  6. #6
    Registered User
    Join Date
    12-20-2014
    Location
    North America
    MS-Off Ver
    2013
    Posts
    40

    Re: Can't pass arguments to a declared class object: obj variable or with block not set

    Thanks for the response. Unfortunately I'm getting run time error 28, out of stack space

    I've gone ahead and reposition the importSheet variable to be the first variable initialized.

    Public Sub InitializeAttributes(sourceVeh_c As String, vehNum_c As String, impFilePrefix_c As String, expFilePrefix_c As String, _
                                 pitGroupPrefix_c As String, pitGroupSuffix_c As String, driver_c As String, descriptionPrefix_c As String, _
                                 classes_c As String, category_c As String, importSheet_c As Worksheet, descriptionDelimeter_c As String)
        
        Set importSheet = importSheet_c
        
        sourceVeh = sourceVeh_c
        impFilePrefix = impFilePrefix_c
        expFilePrefixStr = expFilePrefix_c
        pitGroupPrefix = pitGroupPrefix_c
        pitGroupSuffix = pitGroupSuffix_c
        
        descriptionPrefix = descriptionPrefix_c
        descriptionDelimeter = descriptionDelimeter_c
        
        'None of the following come up in Me.* intellisence suggestion
        'Me.* is not to be used here
        vehNum = vehNum_c
        driver = driver_c
        classes = classes_c
        category = category_c
        
        'It made more sense to place tehse in the Class_Initialize() sub, since they format already initialized variables.
        'Me.* is not to be used here
        defaultLivery(expFilePrefixStr) = vehNum
        pitGroup(pitGroupPrefix) = pitGroupSuffix
        description(descriptionPrefix, descriptionDelimeter) = vehNum
        
        
        'Porential ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        'Me.importSheet = importSheet
    End Sub
    I've also gone ahead and explicitly place the name of the variable in the class initialization object call. The following is highlighted yellow by the code editor during attempt to execute program.
            Dim vehRows8 As vehRows
            Set vehRows8 = New vehRows
            
            
    
            vehRows8.InitializeAttributes sourceVeh_c:=sourceVeh, vehNum_c:=vehNum, impFilePrefix_c:=impFilePrefix, expFilePrefix_c:=expFilePrefix, _
                                        pitGroupPrefix_c:=pitGroupPrefix, pitGroupSuffix_c:=pitGroupSuffix, driver_c:=driver, descriptionPrefix_c:=descriptionPrefix, _
                                        classes_c:=classes, category_c:=category, importSheet_c:=importSheet, descriptionDelimeter_c:=descriptionDelimeter
    I've even went ahead and specified the size of the string variables in the 'fillImportSheet' function of the main class
        Dim sourceVeh As String * 3
        Dim vehNum As String * 3
        Dim impFilePrefix As String * 16
        Dim expFilePrefix As String * 16
        Dim pitGroupPrefix As String * 8
        Dim pitGroupSuffix As String * 2
        Dim driver As String * 32
        Dim descriptionPrefix As String * 32
        Dim classes As String * 16
        Dim category As String * 48
    Unfortunately the exact same error was displayed.
    Attached Files Attached Files

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Can't pass arguments to a declared class object: obj variable or with block not set

    You need to turn on the break in class modules option in settings. This will tell you where the terror actually is

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Can't pass arguments to a declared class object: obj variable or with block not set

    It's because you've created an infinite loop here:

    Private Property Let vehNum(vehNum_m As String)
        vehNum = vehNum_m
    End Property

  9. #9
    Registered User
    Join Date
    12-20-2014
    Location
    North America
    MS-Off Ver
    2013
    Posts
    40

    Re: Can't pass arguments to a declared class object: obj variable or with block not set

    Thanks for the assistance. The directions here (link) detailed how to enable break in class module.

    I changed the statement to vehNumStr = vehNum_m
    Last edited by excelicus; 08-20-2018 at 01:43 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Pass variable number of arguments to worksheetfunction
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-17-2016, 10:23 AM
  2. [SOLVED] Run-time error '91': Object variable or With block variable not set when closing userform
    By bishoposiris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2016, 08:59 AM
  3. Replies: 2
    Last Post: 08-22-2014, 03:55 AM
  4. [SOLVED] Excel VBA error code 91: Object variable or with block variable not set
    By wilnexpc in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-24-2013, 11:46 AM
  5. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  6. Replies: 1
    Last Post: 09-25-2012, 08:03 PM
  7. Replies: 0
    Last Post: 09-13-2005, 12:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1