Populate a textbox based on combobox selection and other conditions

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
99
All,

Im trying to figure out why the following code is not working... Im trying to pull data from a sheet into a textbox based on the change of a combobox. See below;

VBA Code:
Private Sub ComboBox10_Change()

Dim i As Long
Dim lastrow As Long
Dim ws As Worksheet

Me.TextBox13 = ""

Set ws = Sheets("paste log")

lastrow = ws.Range("f" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow

If Me.ComboBox11.Value = ws.Cells(i, "b") & Me.ComboBox8.Value = ws.Cells(i, "c") Then

Me.TextBox13 = ws.Cells(i, "f").Text

End If

Next i

End Sub


If i run the following code the code runs, but only brings the latest date in relation to a "NA" line on the spreadsheet and then stops.


VBA Code:
Private Sub ComboBox10_Change()

Dim i As Long
Dim lastrow As Long
Dim ws As Worksheet

Me.TextBox13 = ""

Set ws = Sheets("paste log")

lastrow = ws.Range("f" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow

If Me.ComboBox10.Value = ws.Cells(i, "e") Then

Me.TextBox13 = ws.Cells(i, "f").Text

End If

Next i

End Sub

Any help would be greatly appreciated. Thanks!!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,253
Office Version
  1. 365
Platform
  1. Windows
If the number occurs multiple times then the textbox will have the value from col F for the last occurrence.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
99
If the number occurs multiple times then the textbox will have the value from col F for the last occurrence.
What is i use the following?

VBA Code:
If Me.ComboBox11.Value = ws.Cells(i, "b").Text And Me.ComboBox7.Value = ws.Cells(i, "a").Text Then
 

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
99
What is i use the following?

VBA Code:
If Me.ComboBox11.Value = ws.Cells(i, "b").Text And Me.ComboBox7.Value = ws.Cells(i, "a").Text Then
which still appears to bring back the incorrect value
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,253
Office Version
  1. 365
Platform
  1. Windows
That should get the last occurrence of when when combo11 = col B & combo7 =col A
 

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
99

ADVERTISEMENT

That should get the last occurrence of when when combo11 = col B & combo7 =col A
its not pulling that though. its pulling the last row in column h

VBA Code:
Private Sub ComboBox10_Change()

Dim i As Long
Dim lastrow As Long
Dim ws As Worksheet

'Me.TextBox13 = ""

Set ws = Sheets("paste log")

lastrow = ws.Range("a" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow

If Me.ComboBox11.Value = ws.Cells(i, "b").Text And Me.ComboBox7.Value = ws.Cells(i, "a").Text Then

Me.TextBox13 = ws.Cells(i, "h").Value

End If

Next i

End Sub
 

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
99
Final Code

VBA Code:
Private Sub ComboBox10_Change()

Dim i As Long
Dim lastrow As Long
Dim ws As Worksheet

'Me.TextBox13 = ""

Set ws = Sheets("paste log")

lastrow = ws.Range("a" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow

If Me.ComboBox11.Text = ws.Cells(i, "b").Text And Me.ComboBox7.Text = ws.Cells(i, "a").Text _
And Me.ComboBox8.Text = ws.Cells(i, "c").Text And Me.ComboBox10.Text = ws.Cells(i, "e").Text Then

Me.TextBox13 = ws.Cells(i, "h").Value
Me.TextBox12 = ws.Cells(i, "i").Value

End If

Next i

End Sub

Thank you Fluff!!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,253
Office Version
  1. 365
Platform
  1. Windows
Glad you sorted it & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,132,976
Messages
5,656,189
Members
418,288
Latest member
reba557

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