Display sheet data in userform VBA

Fish fingers

New Member
Joined
Feb 2, 2020
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
I want to extract data from sheet to form and I tried a code that I got from a certain video on YouTube. I wrote mine based on the tutorial and this is what I came up with
```
Private Sub ComboBox1_DropButtonClick()

Dim i As Long, LastRow As Long

LastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

If Me.ComboBox1.ListCount = 0 Then

For i = 2 To LastRow

Me.ComboBox1.AddItem Sheets("sheet1").Cells(i, "A").Value

Next i

End If

End Sub

Private Sub ComboBox1_Change()

Dim i As Long, LastRow As Long

LastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

If Me.ComboBox1.ListCount = 0 Then

For i = 2 To LastRow

If Sheets("sheet1").Cells(i, "A").Value = Val(Me.ComboBox1) Then

Me.TextBox1 = Sheets("sheet1").Cells(i, "B").Value

Me.TextBox2 = Sheets("sheet1").Cells(i, "C").Value

Me.TextBox3 = Sheets("sheet1").Cells(i, "D").Value

Me.TextBox4 = Sheets("sheet1").Cells(i, "E").Value

Me.TextBox5 = Sheets("sheet1").Cells(i, "F").Value

Me.TextBox6 = Sheets("sheet1").Cells(i, "G").Value

Me.TextBox7 = Sheets("sheet1").Cells(i, "H").Value

End If

Next

End If

End Sub


Private Sub CommandButton1_Click()
Unload UserForm5
End Sub


Private Sub UserForm_Click()



End Sub
```
The code is running but when I place the dropbutton click on the combobox, am not getting the display of data from the sheet. When I select weights for example. It should show the figures on the corresponding label as shown in the pictures. What's the problem? or maybe someone should just write me a code that works for what I want.
Thanks in advance
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
These are the pictures
 

Attachments

  • Book1 - Excel 03_02_2020 12_00_14 AM.png
    Book1 - Excel 03_02_2020 12_00_14 AM.png
    46.3 KB · Views: 39
  • Book1 - Excel 02_02_2020 11_59_36 PM.png
    Book1 - Excel 02_02_2020 11_59_36 PM.png
    5.7 KB · Views: 41
Upvote 0
This should do it
VBA Code:
Private Sub ComboBox1_Change()

    Dim found As Range
   
    Set found = Sheets("Sheet1").Range("A2", Range("A2").End(xlDown)).Find( _
        What:=ComboBox1.Value, _
        LookIn:=xlValues)
       
    If Not found Is Nothing Then
        TextBox1.Value = found.Offset(, 1)
        TextBox2.Value = found.Offset(, 2)
        TextBox3.Value = found.Offset(, 3)
        TextBox4.Value = found.Offset(, 4)
        TextBox5.Value = found.Offset(, 5)
        TextBox6.Value = found.Offset(, 6)
    End If

End Sub

Private Sub ComboBox1_DropButtonClick()

    Dim cc As Range
   
    If Not ComboBox1.ListCount = 0 Then Exit Sub
   
    For Each cc In Sheets("Sheet1").Range("A2", Range("A2").End(xlDown))
        ComboBox1.AddItem cc.Value
    Next cc

End Sub

Private Sub CommandButton1_Click()
   
    Unload Me
   
End Sub
 
Upvote 0
This should do it
VBA Code:
Private Sub ComboBox1_Change()

    Dim found As Range
  
    Set found = Sheets("Sheet1").Range("A2", Range("A2").End(xlDown)).Find( _
        What:=ComboBox1.Value, _
        LookIn:=xlValues)
      
    If Not found Is Nothing Then
        TextBox1.Value = found.Offset(, 1)
        TextBox2.Value = found.Offset(, 2)
        TextBox3.Value = found.Offset(, 3)
        TextBox4.Value = found.Offset(, 4)
        TextBox5.Value = found.Offset(, 5)
        TextBox6.Value = found.Offset(, 6)
    End If

End Sub

Private Sub ComboBox1_DropButtonClick()

    Dim cc As Range
  
    If Not ComboBox1.ListCount = 0 Then Exit Sub
  
    For Each cc In Sheets("Sheet1").Range("A2", Range("A2").End(xlDown))
        ComboBox1.AddItem cc.Value
    Next cc

End Sub

Private Sub CommandButton1_Click()
  
    Unload Me
  
End Sub
Thank you so much. It has worked as I wanted... I appreciate
 
Upvote 0
This should do it
VBA Code:
Private Sub ComboBox1_Change()

    Dim found As Range
  
    Set found = Sheets("Sheet1").Range("A2", Range("A2").End(xlDown)).Find( _
        What:=ComboBox1.Value, _
        LookIn:=xlValues)
      
    If Not found Is Nothing Then
        TextBox1.Value = found.Offset(, 1)
        TextBox2.Value = found.Offset(, 2)
        TextBox3.Value = found.Offset(, 3)
        TextBox4.Value = found.Offset(, 4)
        TextBox5.Value = found.Offset(, 5)
        TextBox6.Value = found.Offset(, 6)
    End If

End Sub

Private Sub ComboBox1_DropButtonClick()

    Dim cc As Range
  
    If Not ComboBox1.ListCount = 0 Then Exit Sub
  
    For Each cc In Sheets("Sheet1").Range("A2", Range("A2").End(xlDown))
        ComboBox1.AddItem cc.Value
    Next cc

End Sub

Private Sub CommandButton1_Click()
  
    Unload Me
  
End Sub
It is displaying the figures in the textboxes , but now how will I add a code to reduce the number of decimal places to maybe 2 or 3. Currently it's displaying like 10 DCPs.
 
Upvote 0
Try this
VBA Code:
If Not found Is Nothing Then
    TextBox1.Value = Format(found.Offset(, 1), "0.00")
    TextBox2.Value = Format(found.Offset(, 2), "0.00")
    TextBox3.Value = Format(found.Offset(, 3), "0.00")
    TextBox4.Value = Format(found.Offset(, 4), "0.00")
    TextBox5.Value = Format(found.Offset(, 5), "0.00")
    TextBox6.Value = Format(found.Offset(, 6), "0.00")
End If
 
Upvote 0
Worked out perfectly, thanks again
Try this
VBA Code:
If Not found Is Nothing Then
    TextBox1.Value = Format(found.Offset(, 1), "0.00")
    TextBox2.Value = Format(found.Offset(, 2), "0.00")
    TextBox3.Value = Format(found.Offset(, 3), "0.00")
    TextBox4.Value = Format(found.Offset(, 4), "0.00")
    TextBox5.Value = Format(found.Offset(, 5), "0.00")
    TextBox6.Value = Format(found.Offset(, 6), "0.00")
End If
 
Upvote 0

Forum statistics

Threads
1,214,874
Messages
6,122,036
Members
449,062
Latest member
mike575

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