+ Reply to Thread
Results 1 to 12 of 12

loading ListBox row items to another UserForm

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    30

    loading ListBox row items to another UserForm

    It might be eayser if I post all the code here, this first part is Userform4 which does the searching of my data and sets up the results in the listbox as well as performs the listbox selection of a row and then launches userform5 which is the code that's giving me issues. I am getting the error "object Variable or with block variable not set" and the code stops with this line highlighted
    UserForm5.Show vbModeless

    This is UserForm4 getting error as indicated in Red

    Private Sub cmbSelect_Click()
    
    
        If Me.ListBox1.ListIndex = -1 Then
            MsgBox " No selection made"
        ElseIf Me.ListBox1.ListIndex >= 0 Then
            UserForm4.Hide
            rw = UserForm4.ListBox1.ListIndex
            UserForm5.Show vbModeless
        End If
        
    End Sub
    Private Sub CommandButton1_Click()
    
    
    Dim sFindText As String, sFirstAddr As String
        Dim i As Long, lFindCol As Long, lCol As Long, lRow As Long
        Dim rMyData As Range, rMySearchField As Range, cFound As Range
        Dim vArray() As Variant
       
    
    
    
    
    With Worksheets("Membership Sales")
    .Unprotect Password:="taylor97"
    End With
    
    
    '--check User has entered Find_Text
        sFindText = Trim(TextBox1.Value)
        If sFindText = "" Then Exit Sub  'could add msgbox or label
    
    
        '--read user's choice of field to search
        '  (considered using a listbox instead of option buttons)
        With Frame1.Controls
            lFindCol = 5 'Default if no options buttons True
            For i = 0 To .Count - 1
                If .Item(i).Value = True Then
                    Select Case .Item(i).Name
                        Case "OptionButton1": lFindCol = 5
                        Case "OptionButton2": lFindCol = 6
                        Case "OptionButton3": lFindCol = 1
                        Case "OptionButton4": lFindCol = 20
                    End Select
                    Exit For
                End If
            Next i
        End With
    
    
        With Worksheets("Membership Sales")
            Set rMyData = .Range("A2:V500")
            Set rMySearchField = rMyData.Resize(, 1).Offset(0, lFindCol - 1)
        End With
    
    
                
        With rMySearchField
            On Error Resume Next
            Set cFound = .Find(What:=sFindText, After:=.Cells(.Rows.Count), _
                LookIn:=xlFormulas, LookAt:=CheckBox2.Value + 2, _
                SearchDirection:=xlNext, MatchCase:=CheckBox1.Value)
            On Error GoTo 0
    
    
            If cFound Is Nothing Then
                MsgBox "No Match was found for '" & sFindText & " '", vbExclamation
            Else
                sFirstAddr = cFound.Address
    
    
                
                With rMyData
                '--set the boundaries of the array
                    ReDim vArray(1 To .Columns.Count, 1 To .Rows.Count)
                    Do Until cFound Is Nothing
                     '--add matching records from worksheet row to array
                        lRow = lRow + 1
                        For lCol = 1 To UBound(vArray, 1)
                            vArray(lCol, lRow) = .Cells(cFound.Row - .Row + 1, lCol + .Column - 1)
                        Next lCol
    
    
                        
                        Set cFound = rMySearchField.FindNext(After:=cFound)
                        If cFound.Address = sFirstAddr Then Exit Do
                    Loop
                    '--resize array to fit records stored.
                    'ReDim Preserve vArray(1 To .Columns.Count, 1 To lRow)
                    'the If statement was add so the first record would fill listbox horizontily
                     If lRow = 1 Then lRow = 2
                        ReDim Preserve vArray(1 To .Columns.Count, 1 To lRow)
                End With
    
    
                
                '--place the array in the listbox
                With Me.ListBox1
                    .ColumnCount = UBound(vArray, 1)
                    .ColumnWidths = "50;;80;100" 'use this property to override default col widths
                    .List = Application.Transpose(vArray)
                End With
            End If
        End With
        
    
    
    
    
    With Worksheets("Membership Sales")
    .Protect Password:="taylor97"
    End With
    
    
    
    
    End Sub
    Private Sub CommandButton2_Click()
    'CLOSE
        Me.Hide
        Unload Me
    End Sub
    
    
    Private Sub CommandButton3_Click()
    'RESET FORM
        TextBox1.Value = ""
        OptionButton1.Value = True
        CheckBox1.Value = False
        CheckBox2.Value = False
        CheckBox3.Value = True
        ListBox1.Clear
        TextBox1.SetFocus
    End Sub
    
    
    Private Sub OptionButton1_Click()
        If OptionButton1.Value = True Then
            Label1.Caption = "Surname"
            TextBox1.Value = ""
            TextBox1.SetFocus
        End If
    End Sub
    Private Sub OptionButton2_Click()
        If OptionButton2.Value = True Then
            Label1.Caption = "Address Number and Street"
            TextBox1.Value = ""
            TextBox1.SetFocus
        End If
    End Sub
    Private Sub OptionButton3_Click()
        If OptionButton3.Value = True Then
            Label1.Caption = "Registration #"
            TextBox1.Value = ""
            TextBox1.SetFocus
        End If
    End Sub
    Private Sub OptionButton4_Click()
        If OptionButton4.Value = True Then
            Label1.Caption = "KeyFOB Number"
            TextBox1.Value = "AD:"
            TextBox1.SetFocus
        End If
    End Sub
    Private Sub UserForm_Activate()
        CommandButton3.Value = True
    End Sub
    Private Sub UserForm_Initialize()
        ListBox1.Clear
    End Sub
    This is UserForm5 that has a bunch of TextBox and ComboBox controls to be populated from the Userform4 listbox once launched.

    Private Sub UserForm_Initialize()
    
    
        Dim rw As Long
        Dim rMyData As Range
        rw = UserForm4.ListBox1.ListIndex
        If rw = -1 Then
            MsgBox "Nothing selected in ListBox1"
        Else
            With rMyData.Range(UserForm4.ListBox1.List(rw, 0))
                TextBox2.Value = .Offset(0, 4)
                TextBox3.Value = .Offset(0, 5)
                TextBox40.Value = .Offset(0, 6)
                TextBox41.Value = .Offset(0, 7)
                TextBox42.Value = .Offset(0, 8)
                TextBox43.Value = .Offset(0, 9)
                TextBox44.Value = .Offset(0, 10)
                TextBox4.Value = .Offset(0, 11)
                TextBox5.Value = .Offset(0, 12)
                TextBox6.Value = .Offset(0, 13)
            End With
        End If
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: loading ListBox row items to another UserForm

    The line of code you highlight probably isn't where the error is.

    The error is more likely to be in the Initialize of UserForm5.

    To find it goto Tools>Options and on the General tab change to either Break on Error in Class Modules or Break on all Errors.

    Might be worth attaching the workbook.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: loading ListBox row items to another UserForm

    Your right about attaching the file, as I still can't this to work, I hope this helps if you can resolve this one, I have been on this for days and just can't get it.
    Attached Files Attached Files

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: loading ListBox row items to another UserForm

    You haven't set rMyData to anything in the UserForm5 module so you get an error here.
     With rMyData.Range(UserForm4.ListBox1.List(rw, 0))
    You do declare it and set it to refer to a range in the sub for the search button on UserForm4, but it won't be visible outside that sub.

  5. #5
    Registered User
    Join Date
    02-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: loading ListBox row items to another UserForm

    It's still not liking that, perhaps I am missing the boat on where to declare it

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: loading ListBox row items to another UserForm

    What did you try?

    You have declared it, just not set it to refer to anything.


    What should it refer to?

  7. #7
    Registered User
    Join Date
    02-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: loading ListBox row items to another UserForm

    I put it here in UserForm5

    Private Sub UserForm_Initialize()
    
        Dim rw As Long
        Dim rMyData As Range
        rw = rMyData.Range(UserForm4.ListBox1.List)
        If rw = -1 Then
            MsgBox "Nothing selected in ListBox1"
        Else
            With rMyData.Range(UserForm4.ListBox1.ListIndex(rw, 0))
                TextBox2.Value = .Offset(0, 4)
                TextBox3.Value = .Offset(0, 5)
                TextBox40.Value = .Offset(0, 6)
                TextBox41.Value = .Offset(0, 7)
                TextBox42.Value = .Offset(0, 8)
                TextBox43.Value = .Offset(0, 9)
                TextBox44.Value = .Offset(0, 10)
                TextBox4.Value = .Offset(0, 11)
                TextBox5.Value = .Offset(0, 12)
                TextBox6.Value = .Offset(0, 13)
            End With
        End If

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: loading ListBox row items to another UserForm

    I thought you'd changed something?

    You need to set rMyData to refer to a range.

    In the other code you do that here.
            Set rMyData = .Range("A2:V500")
    What are you trying to populate the textboxes with anyway?

    Is it the data from the selected row on UserForm4?
    Private Sub UserForm_Initialize()
    
    Dim rw As Long
    Dim rMyData As Range
        rw = UserForm4.ListBox1.ListIndex
        If rw = -1 Then
            MsgBox "Nothing selected in ListBox1"
        Else
    
            TextBox2.Value = UserForm4.ListBox1.List(rw, 3)
            TextBox3.Value = UserForm4.ListBox1.List(rw, 4)
            TextBox40.Value = UserForm4.ListBox1.List(rw, 5)
            TextBox41.Value = UserForm4.ListBox1.List(rw, 6)
            TextBox42.Value = UserForm4.ListBox1.List(rw, 7)
            TextBox43.Value = UserForm4.ListBox1.List(rw, 8)
            TextBox44.Value = UserForm4.ListBox1.List(rw, 9)
            TextBox4.Value = UserForm4.ListBox1.List(rw, 10)
            TextBox5.Value = UserForm4.ListBox1.List(rw, 11)
            TextBox6.Value = UserForm4.ListBox1.List(rw, 12)
    
        End If
    End Sub
    Last edited by Norie; 12-27-2012 at 01:53 AM.

  9. #9
    Registered User
    Join Date
    02-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: loading ListBox row items to another UserForm

    yes that's it I am trying to populate the UserForm5 textBoxes with the row data in the ListBox1 on UserForm4

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: loading ListBox row items to another UserForm

    That's what that last piece of code I posted does.

  11. #11
    Registered User
    Join Date
    02-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: loading ListBox row items to another UserForm

    Resolved!!

    Thank you for the help, I had been trying so many things I cant remember what I tried first or last, But this is the resluting code that now works and I could not have gotten her without your help

    Private Sub UserForm_Initialize()
    
       Dim rw As Long
        Dim rMyData As Range
        rw = UserForm4.ListBox1.ListIndex
        Set rMyData = Range("A2:V500")
        If rw = -1 Then
            MsgBox "Nothing selected in ListBox1"
        Else
            With UserForm5
                TextBox2.Value = UserForm4.ListBox1.List(rw, 3)
                TextBox3.Value = UserForm4.ListBox1.List(rw, 4)
                TextBox40.Value = UserForm4.ListBox1.List(rw, 5)
                TextBox41.Value = UserForm4.ListBox1.List(rw, 6)
                TextBox42.Value = UserForm4.ListBox1.List(rw, 7)
                TextBox43.Value = UserForm4.ListBox1.List(rw, 8)
                TextBox44.Value = UserForm4.ListBox1.List(rw, 9)
                TextBox4.Value = UserForm4.ListBox1.List(rw, 10)
                TextBox5.Value = UserForm4.ListBox1.List(rw, 11)
                TextBox6.Value = UserForm4.ListBox1.List(rw, 12)
    
            End With
        End If
    End Sub

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: loading ListBox row items to another UserForm

    thank you for posting the resolution :-) please don't forget to mark the thread solved
    Josie

    if at first you don't succeed try doing it the way your wife told you to

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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