Listbox data to text box

isaacv22

New Member
Joined
Sep 30, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm currently trying to pull data from listbox2 on userform2 to text boxes in userform1. Listbox2 is populated based on a selection/click made on (userform2) listbox1. Listbox2 breaks down the value selected on listbox1 into 1 row and 5 columns.
VBA Code:
Private Sub ListBox1_Click()


Dim LastRow As Integer, CurVal As Variant, X As Integer
'Find last row
LastRow = form.Cells(Rows.Count, "k").End(xlUp).Row

'Clear listbox1


CurVal = Me.ListBox1.Value

For X = 2 To LastRow
With Sheets("form")
    If .Cells(X, "k") = CurVal Then
        'Found a match; populate listbox2
         Me.ListBox2.RowSource = .Range(.Cells(X, "F"), Cells(X, "J")).Address
Exit For
End If
End With
Next X
End Sub

Private Sub Select1_Click()


  Dim unit As String

    UserForm1.txtMaterial.Value = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 1)
    
    UserForm1.txtSeries.Value = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 2)

    UserForm1.txtSurface.Value = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 3)

    UserForm1.txtWidth.Value = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 4)

    unit = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 5)
    
    If measure = "IN" Then 'Edit fucntion checks the checkbox that was previously selected before edit.
        
        UserForm1.CbIN.Value = True
    Else
        UserForm1.CbMM.Value = True
        
    End If
    
    MsgBox "Please Make the required changes and click on 'Save' button to update.", vbOKOnly + vbInformation, "Select"

End Sub
I'm trying to pull those values into specific textboxes on userform 1. Below is the code I'm using and the error i keep getting is invalid use of null
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
3,090
Are you hiding Userform1 when using Userform2? On what line of code does the error occur? "measure" doesn't seem to be defined anywhere? What are Cbin and CbMM? Are they comboboxes? I don't think that U can set a combobox value to true.... I've been wrong before. This line of code can be improved...
Code:
LastRow = Sheets("form").Cells(Rows.Count, "k").End(xlUp).Row
HTH. Dave
 

isaacv22

New Member
Joined
Sep 30, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Are you hiding Userform1 when using Userform2? On what line of code does the error occur? "measure" doesn't seem to be defined anywhere? What are Cbin and CbMM? Are they comboboxes? I don't think that U can set a combobox value to true.... I've been wrong before. This line of code can be improved...
Code:
LastRow = Sheets("form").Cells(Rows.Count, "k").End(xlUp).Row
HTH. Dave
Hello Dave,

I apologize for the delay in response. Userform1 is not hidden when userform2 is being used. CBin and CBMM are checkboxes on UserForm1. The area where I'm having issues is highlighted below. When I try to run the code step by step it says Object variable or With block variable not set.
VBA Code:
Private Sub Select1_Click()

If UserForm2.ListBox2 = 0 Then
    
        MsgBox "No Row has been selected.", vbInformation, "Select"
        
        Exit Sub
    
    End If

  Dim unit As String

   [U][B] UserForm1.txtMaterial.Value = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 1)[/B][/U]
    
    UserForm1.txtSeries.Value = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 2)

    UserForm1.txtSurface.Value = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 3)

    UserForm1.txtWidth.Value = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 4)

    unit = UserForm2.ListBox2.List(UserForm2.ListBox2.ListIndex, 5)
    
    If unit = "IN" Then 'Edit fucntion checks the checkbox that was previously selected before edit.
        
        UserForm1.CbIN.Value = True
    Else
        UserForm1.CbMM.Value = True
        
    End If
    
    MsgBox "Please Make the required changes and click on 'Save' button to update.", vbOKOnly + vbInformation, "Select"
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
3,090
Instead of unloading Userform1, hide it when showing Userform2. HTH. Dave
 

Forum statistics

Threads
1,176,598
Messages
5,903,961
Members
435,061
Latest member
CGA

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
Top