Two column combobox

Rick Trenidad

New Member
Joined
Apr 23, 2020
Messages
5
Office Version
  1. 2013
Platform
  1. Windows
Hi, I’m Rick Trenidad. I have a question. I have a sheet sheet1 that has in column B a unique number and in Column G a text! The text in Colunm G can appear many times. It’s the last name of a person. So this person can have many cases going! My question is is it possible to create a combobox showing the unique number and the lastname? The reason I’m asking is that I do have a combobox with lastname but when selection is made only the last lastname selected is shown in the userform. I hope you can help me with this problem! Thanks Rickshaws
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Something like this will fill the ComboBox.

VBA Code:
Private Sub UserForm_Initialize()
    Dim oneCell as Range

    With ComboBox1
        .TextColumn = 1
        .BoundColumn = 2
        For each oneCell in Range(Cells(1, 2), Cells(Rows.Count,2).End(xlUp))
            .AddItem oneCell.Value
            .List(.ListCount - 1, 1) = oneCell.EntireRow.Range("G1").Value
        Next oneCel
    End With
End Sub

And the .BoundColumn will determine which column's value is returned by the .Value property. The .TextColumn property will determine what value is shown in the box and which is returned by the .Text property.


VBA Code:
With ComboBox1
    MsgBox .Text & " from column B was selected, because .TextColumn= 1"
    MsgBox .Value & " from column G was selected, since .BoundColumn = 2"
End With
 
Upvote 0
Good morning mrExcel MVP, the combobox is working! But I actually want it now to fill the userform! I’ll send a picture of the form! The labels are in Dutch. Sir I hope you can help me with this part
 

Attachments

  • A2F42F63-CB2F-4B36-A0E3-DCF6371A1A5C.jpeg
    A2F42F63-CB2F-4B36-A0E3-DCF6371A1A5C.jpeg
    81.4 KB · Views: 10
Upvote 0
I don't read Dutch, but I'm guessing that you want all those other controls to be filled with data once the user makes a selection from the (two columned) Zoeken OP# combobox.

I'm also guessing that that data is stored on a worksheet. The first thing to do would be to find what row the selected record is on.
I would amend my code above to include the address of the source row as a hidden third column of the combobox.

Rich (BB code):
Private Sub UserForm_Initialize()
    Dim oneCell as Range

    With ComboBox1
        .ColumnCount = 3
        .ColumnWidths = ";;0"
        .TextColumn = 1
        .BoundColumn = 2
        For each oneCell in Range(Cells(1, 2), Cells(Rows.Count,2).End(xlUp))
            .AddItem oneCell.Value
            .List(.ListCount - 1, 1) = oneCell.EntireRow.Range("G1").Value
            .List(.ListCount - 1, 2) = oneCell.EntireRow.Address(,,,True)
        Next oneCell
    End With
End Sub

That would be used by the ComboBox change event to fill the other controls.

VBA Code:
Private Sub ComboBox1_Change()
    Dim rngRowSelected As Range

    With ComboBox1
        If .ListIndex <> -1 Then
            Set rngRowSelected = Range(.List(.ListIndex, 2)).EntireRow
        End If
    End With

    If rngRowSelected Is Nothing Then
        TextBox1.Text = vbNullString
        TextBox2.Text = vbNullString
        'etc.
    Else
        With rngRowSelected
            TextBox1.Text = .Range("A1").Text
            TextBox2.Text = .Range("C1").Text
            'etc.
        End With
    End If
End Sub
The correspondence between column and control (TextBox1 is column A values, TextBox2 is column C,...) would need to be adjusted to match your situation.
 
Upvote 0
Many thanks mrExcel MVP it worked perfectly! And the language wasn’t a problem for you! Actually I’m from the beautiful island of Curacao a Dutch Caribbean island!
I’m also very glad to have registered to the webblink of MrExcelMVP.
Again thanks! I wouldn’t have struggled alot to get this done and you did it in minutes! Unbelievable! Thank you very much!
Rick Trenidad
 
Upvote 0
Dear MREXCEL MVP
I’m having a problem with the two columns dropdown. When I select the number and the name the search is made and correct fills all the textboxes etc. When the command UPDATE button is chosen all the fields are ok and the fields that are edited as well. But the number field in column B is changed in the name of column G. How can I avoid this?
 

Attachments

  • 39C621C6-AF69-4B21-947A-6B6C1B26B7B7.jpeg
    39C621C6-AF69-4B21-947A-6B6C1B26B7B7.jpeg
    24.8 KB · Views: 7
  • 0704CD85-4C2A-48FD-AA8D-74D5F916236F.jpeg
    0704CD85-4C2A-48FD-AA8D-74D5F916236F.jpeg
    25.8 KB · Views: 4
  • 6C584EA8-B01B-4A70-B7A5-5EA4A1B3661D.jpeg
    6C584EA8-B01B-4A70-B7A5-5EA4A1B3661D.jpeg
    24.3 KB · Views: 8
  • 4B378C1F-3CAB-4602-9232-345B4D23AF77.jpeg
    4B378C1F-3CAB-4602-9232-345B4D23AF77.jpeg
    24.6 KB · Views: 5
  • AA22D8AB-7FD3-4EF5-A7A4-62EEFBC4BBA9.jpeg
    AA22D8AB-7FD3-4EF5-A7A4-62EEFBC4BBA9.jpeg
    21.2 KB · Views: 4
Upvote 0

Forum statistics

Threads
1,215,544
Messages
6,125,444
Members
449,226
Latest member
Zimcho

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