Adding multiple lines of text to textbox from combobox selection

isaacv22

New Member
Joined
Sep 30, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hello I'm trying add multiple lines of text to text box based off of values in ComboBox1 and 2. I get the text to show up when I make a selections on combobox1 but when an additional value on combobox2 is made it overwrites the text instead of add it to a new line. Below is the code, any assistance will be greatly appreciated.
VBA Code:
Private Sub ComboBox2_Change()

Dim i As Long, LastRow As Long, ws As Worksheet

    Set ws = Sheets("Sheet2")
    
    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
    
        For i = 2 To LastRow
            If Me.ComboBox2.Value = ws.Cells(i, "A") Then
          
            Me.TextBox1 = vbNewLine & ws.Cells(i, "B").Value
            
            Me.TextBox2 = vbNewLine & ws.Cells(i, "c").Value
            
    End If
    
    Next i

End Sub

Private Sub ComboBox1_Change()

Dim i As Long, LastRow As Long, ws As Worksheet

    Set ws = Sheets("Sheet2")
    
    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
    
        For i = 2 To LastRow
            If Me.ComboBox1.Value = ws.Cells(i, "A") Then
          
            Me.TextBox1 = ws.Cells(i, "B").Value
            Me.TextBox2 = ws.Cells(i, "c").Value
            
        ElseIf Me.ComboBox1.Value = "" Then
        
            Me.TextBox1.Text = ""
            Me.TextBox2.Text = ""
                
            
            
    End If
    
    
    Next i
    
End Sub
Thanks,
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
For i = 2 To LastRow If Me.ComboBox2.Value = ws.Cells(i, "A") Then Me.TextBox1 = vbNewLine & ws.Cells(i, "B").Value Me.TextBox2 = vbNewLine & ws.Cells(i, "c").Value End If
If you want to add to the textbox, you need to include the textbox text
Something like
VBA Code:
textbox1=textbox1 & vbnewline & newText
 
Upvote 0
Dave,

I put that code in comboBox2_Change and it didn't work. I believe it's because the values selected in both combobox 1 &2 are the values form column A from a table I'm referencing on sheet2. When a value matches what is in column A it will pull the respective text from columns B and C. Not sure how much this helps, but I figured I'd try and more detail of what i'm trying to do with this code.
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,007
Members
448,935
Latest member
ijat

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