Consulting

Results 1 to 5 of 5

Thread: Userform Populated with range from another excel file

  1. #1
    VBAX Regular
    Joined
    Nov 2012
    Posts
    57
    Location

    Userform Populated with range from another excel file

    Hello,

    Trying to automate the generation of templates in Word. I am creating a userform with combo boxes and I would like the combo boxes to be populated from ranges in a separate excel file, which will be closed once the variables are assigned to the ranges (without the user ever seeing the excel file).

    First, the below runs from a button on a ribbon:

    Sub GenerateLetter()
    '
    ' GenerateLetter Macro
    '
    '    
    
    UserInput.Show
    
    
    End Sub
    Then, the UserInput form is shown and the code for its initialization looks like:

    Private Sub UserForm_Initialize()
    
        Dim oExcel As Object
        Dim oWB As Object
        Dim SCNumberRange As Range
        Dim LastRow As Long
        
        Set oExcel = CreateObject("Excel.Application")
        Set oWB = oExcel.Workbooks.Open("C:\Users\jsabo\Documents\Letter Generator\Admin\Subcontract_List.xlsx")
        oExcel.Visible = True
        
        With oWB
    
            LastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            SCNumberRange = oWB.Sheets("Data").Range("A2:A" & LastRow)
            
        End With
        
    End Sub
    I get a runtime 424 'Object Required' error. Currently, both the userform and the module are both under the "Normal" project. Any ideas?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Set SCNumberRange = oWB.Sheets("Data").Range("A2:A" & LastRow)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Nov 2012
    Posts
    57
    Location
    Hm, doesn't seem to have worked. another forum suggested "LastRow = .ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row" but that failed to work as well.

  4. #4
    VBAX Regular
    Joined
    Nov 2012
    Posts
    57
    Location
    Have been working on this for hours and have refined it to:

    Private Sub UserForm_Initialize()
    
        Dim oExcel As Object
        Dim oWB As Object
        Dim SCNumberRange As Range
        Dim LastRow As Long
        
        Set oExcel = CreateObject("Excel.Application")
        Set oWB = oExcel.Workbooks.Open("C:\Users\jsabo\Documents\Letter Generator\Admin\Subcontract_List.xlsx")
        oExcel.Visible = True
        
        With oWB.Sheets("Data")
    
            LastRow = .UsedRange.Rows.Count
            Set SCNumberRange = .Range("A2:A" & LastRow)
            
        End With
        
    End Sub
    But still have an error- this time a "Type Mismatch" error??

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Where is the error occurring?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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