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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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
 
Upvote 0
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"
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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
Back
Top