+ Reply to Thread
Results 1 to 4 of 4

Private Function within class module/object, can't pass value of range as string

Hybrid View

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

    Private Function within class module/object, can't pass value of range as string

    I have the following function in the class module vehrows

    Private Function extractOrginalSourceVehNum() As String
        Dim impFP_searchRng As Range: Set impFP_searchRng = tableSheet.Range("C1:C" & lastRowOfTableSheet)
          
        'It's possible for 2 different car models to have same SourceVeh ID
        'Search for 1st impFilePrefix row, then search for 1st occurence of sourceVeh and extract original vehNum
        Dim impFilePrefix_Cell As Range: Set impFilePrefix_Cell = impFP_searchRng.Find(What:=impFilePrefixStr, lookat:=xlWhole, After:=impFP_searchRng(impFP_searchRng.Count))
        
        'Allows us to start searching at the first instance where impfilePrefix was obtained.
        Dim sVeh_searchRng As Range: Set sVeh_searchRng = tableSheet.Range(Cells(impFilePrefix_Cell.Row, 1), Cells(lastRowOfTableSheet, 1))   '1 is columnA
        
        Dim sourceVehCell As Range: Set sourceVehCell = sVeh_searchRng.Find(What:=sourceVeh, lookat:=xlWhole)
        
        'Dim result As String: result = sourceVehCell.Offset(, 1).Value
        
        extractOrginalSourceVehNum = sourceVehCell.Offset(, 1).Value
    End Function
    It its called from the alternate constructor of the class

    Public Sub InitializeAttributes(...)
        sourceVehCell_vehNum = extractOrginalSourceVehNum       'Original vehNum of derived SourceVeh File, must be placed after 'lastRowOfTableSheet ='
    End Sub
    I get error 91. I then go ahead and place set in front of the statement
    Set sourceVehCell_vehNum= sourceVehCell.Offset(, 1).Value
    It then states Object Required for sourceVehCell_vehNum.

    I had it combined with another function, the problem did not occur, i.e.

    'Type B' selected in TableSheet, will trigger this function
    'This is updated, because a veh file may have a suffix totally different from "VehNum"
    Private Function updateSourceVeh()
        Dim lastRowOfTableSheet As Integer
        lastRowOfTableSheet = tableSheet.Range("A" & Rows.Count).End(xlUp).Row
    
        Dim impFP_searchRng As Range: Set impFP_searchRng = tableSheet.Range("C1:C" & lastRowOfTableSheet)
          
        'It's possible for 2 different car models to have same SourceVeh ID
        'Search for 1st impFilePrefix row, then search for 1st occurence of sourceVeh and extract original vehNum
        Dim impFilePrefix_Cell As Range: Set impFilePrefix_Cell = impFP_searchRng.Find(What:=impFilePrefixStr, lookat:=xlWhole, After:=impFP_searchRng(impFP_searchRng.Count))
        
        'If impFilePrefix for each car model is only specified once in the corresponding row, be sure to make sure loop doesn't exceed last row, if necessary
        
        'Allows us to start searching at the first instance where impfilePrefix was obtained.
        Dim sVeh_searchRng As Range: Set sVeh_searchRng = tableSheet.Range(Cells(impFilePrefix_Cell.Row, 1), Cells(lastRowOfTableSheet, 1))   '1 is columnA
        
        Dim sourceVehCell As Range: Set sourceVehCell = sVeh_searchRng.Find(What:=sourceVeh, lookat:=xlWhole)
     
        'tableSheet.Range("K2").Value = sourceVehCell.Value
        
        Dim sourceVehCell_vehNum As String: sourceVehCell_vehNum = sourceVehCell.Offset(, 1).Value           'Original veh num of derived SourceVeh
        
        'If vehNum matches orgiVehNum, don't run
        If sourceVehCell_vehNum <> vehNum Then
            If Len(sourceVehCell_vehNum) <> Len(vehNum) Then
                'VehNum was derived from sourVeh, right... right?
                sourceVehCell_vehNum = enlargeOrigVehNum(sourceVeh, sourceVehCell_vehNum, vehNum)
            End If
            
            'Replace origVehNum in sourceVeh with vehNum, by extracting values left of, and the appending it.
            Dim tempSourceVeh As String: tempSourceVeh = truncateSourceVeh(sourceVeh, sourceVehCell_vehNum) + vehNum
            
            sourceVeh = tempSourceVeh + Right(sourceVeh, Len(sourceVeh) - Len(tempSourceVeh))
        End If
    End Function
    Attached Files Attached Files

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Private Function within class module/object, can't pass value of range as string

    You would use Set in conjunction with objects. The return value is a string therefore uses Let instead. And Let is optional.

    Why do you think you should, or want to, be using Set?
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,021

    Re: Private Function within class module/object, can't pass value of range as string

    I don't get that error with your files (after removing the Set). I do note though that you never check to see if your Find operations succeeded before using the found cell, which is bad practice.

    Also note that code like this can fail:

    Set sVeh_searchRng = tableSheet.Range(Cells(impFilePrefix_Cell.Row, 1), Cells(lastRowOfTableSheet, 1))
    It needs to be:

    Set sVeh_searchRng = tableSheet.Range(tableSheet.Cells(impFilePrefix_Cell.Row, 1), tableSheet.Cells(lastRowOfTableSheet, 1))
    Everyone who confuses correlation and causation ends up dead.

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

    Re: Private Function within class module/object, can't pass value of range as string

    Quote Originally Posted by rorya View Post
    I don't get that error with your files (after removing the Set). I do note though that you never check to see if your Find operations succeeded before using the found cell, which is bad practice.
    Removing set get generates error 91 on my end.

        extractOrginalSourceVehNum = sourceVehCell.Offset(, 1).Value   'Line 390
    I use the the debugger, and soruceVehCell is pointing to the correct cell that holds the value, and vba is refusing to pass the value to the string variable. Sheet3 shows what Sheet2 is supposed to look like.

    I the following file (attached to this post), I'm able to assign to do extractOrginalSourceVehNum = sourceVehCell.Offset(, 1).Value, without that error message.

    I even got rid of "As String", in the initial spreadsheet I uploaded, and the same problem still occurs.

    As stated, this function and it's result is meant to be only called within the class module, not outside of it.

    I went ahead and implemented property let and property get
    Private Property Set e_extractOrginalSourceVehNum(tableSheet As Range)
        Dim impFP_searchRng As Range: Set impFP_searchRng = tableSheet.Range("C1:C" & lastRowOfTableSheet)
          
        'It's possible for 2 different car models to have same SourceVeh ID
        'Search for 1st impFilePrefix row, then search for 1st occurence of sourceVeh and extract original vehNum
        Dim impFilePrefix_Cell As Range: Set impFilePrefix_Cell = impFP_searchRng.Find(What:=impFilePrefixStr, lookat:=xlWhole, After:=impFP_searchRng(impFP_searchRng.Count))
        
        'Allows us to start searching at the first instance where impfilePrefix was obtained.
        Dim sVeh_searchRng As Range: Set sVeh_searchRng = tableSheet.Range(tableSheet.Cells(impFilePrefix_Cell.Row, 1), tableSheet.Cells(lastRowOfTableSheet, 1))   '1 is columnA
        
        Dim sourceVehCell As Range: Set sourceVehCell = sVeh_searchRng.Find(What:=sourceVeh, lookat:=xlWhole)
        
        Set sourceVehCell_vehNum = sourceVehCell.Offset(, 1)
    End Property
    
    Private Property Get e_extractOrginalSourceVehNum() As Range
        Let e_extractOrginalSourceVehNum = sourceVehCell_vehNum
    End Property
    And I'm getting the same error message, but in a different function

    Private Function updateSourceVeh()
        'If vehNum matches orgiVehNum, don't run
        If sourceVehCell_vehNum.Value <> vehNum Then
            If Len(sourceVehCell_vehNum.Value) <> Len(vehNum) Then
                'VehNum was derived from sourVeh, right... right?
                sourceVehCell_vehNum.Value = enlargeOrigVehNum(sourceVeh, sourceVehCell_vehNum.Value, vehNum)
            End If
            
            'Replace origVehNum in sourceVeh with vehNum, by extracting values left of, and the appending it.
            Dim tempSourceVeh As String: tempSourceVeh = truncateSourceVeh(sourceVeh, sourceVehCell_vehNum.Value) + vehNum
            
            sourceVeh = tempSourceVeh + Right(sourceVeh, Len(sourceVeh) - Len(tempSourceVeh))
        End If
    End Function
    I think I found the cause.

    In the ClassIntialize Function, I needed to reposition a Property Let/Get function, i.e. exportFileName(expFilePrefixStr) = vehNum

    to below the sourceVehCell_vehNum = extractOrginalSourceVehNum initialization, since the former was calling a function, dependent on the result from the initialization of sourceVehCell_vehNum.

    The debugger was displaying a sourceVeh number that was non-existent in Column A.
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by excelicus; 08-31-2018 at 05:56 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] Can't pass arguments to a declared class object: obj variable or with block not set
    By excelicus in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-20-2018, 01:18 PM
  2. [SOLVED] Using Application.Run to call a private sub in a module from ThisWorkbook object
    By Stormin' in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-24-2017, 10:47 AM
  3. Pass Boolean in Class Module
    By krishns in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-11-2015, 04:38 AM
  4. Trying to use the worksheet object inside a vba class module
    By dbwstein in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-07-2015, 09:52 PM
  5. [SOLVED] Importing .BAS to Sheet Object... NOT Class Module?
    By crabby09 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2014, 10:54 AM
  6. [SOLVED] Pass checkbox variable to private module in this workbook.
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-06-2013, 11:14 AM
  7. [SOLVED] To access a previous object via class module
    By Pierre Archambault in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-31-2005, 02:51 AM

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