TextBox values from datasheet

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
This code displays the correct value in Textbox1...
But the remaining textboxes display values from the last used row of the datasheet. What am I doing wrong?

Code:
Private Sub ComboBox1_Change()
Dim x&
With Sheets("Sheet1")
For x = 1 To .Cells(Rows.Count, "D").End(xlUp).Row
If .Cells(x, "D").Value = Me.ComboBox1.Value Then _
Me.TextBox1.Value = .Cells(x, "F").Value
Me.TextBox2.Value = .Cells(x, "G").Value
Me.TextBox3.Value = .Cells(x, "H").Value
Me.TextBox4.Value = .Cells(x, "I").Value
Me.TextBox5.Value = .Cells(x, "J").Value
Next x
End With
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Are these multi-line textboxes? If so maybe you need something like

Code:
Me.TextBox1.AddItem = .Cells(x, "F").Value
 
Upvote 0
No, TextBoxes are single line.

Datasheet has several hundred rows...Names addresses etc...

The code displays the correct "Address" in TB1 but all remaining TB displays values from the last row of data
 
Upvote 0
OK...This works, but I'm thinking it could be tidier???

Code:
Private Sub ComboBox1_Change()
Dim x&
With Sheets("Sheet1")
    For x = 1 To .Cells(Rows.Count, "D").End(xlUp).Row
        If .Cells(x, "D").Value = Me.ComboBox1.Value Then _
            Me.TextBox1.Value = .Cells(x, "F").Value
        If .Cells(x, "D").Value = Me.ComboBox1.Value Then _
            Me.TextBox2.Value = .Cells(x, "G").Value
        If .Cells(x, "D").Value = Me.ComboBox1.Value Then _
            Me.TextBox3.Value = .Cells(x, "H").Value
        If .Cells(x, "D").Value = Me.ComboBox1.Value Then _
            Me.TextBox4.Value = .Cells(x, "I").Value
        If .Cells(x, "D").Value = Me.ComboBox1.Value Then _
            Me.TextBox5.Value = .Cells(x, "J").Value
    Next x
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,754
Members
452,940
Latest member
rootytrip

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