VBA: ComboBox

Stromma

Board Regular
Joined
Feb 8, 2004
Messages
240
Hi All

In a UserForm i have 2 ComboBoxes.

ComboBox1 List = Sheets("Artikel").Range("A2:A1000")
ComboBox2 List = Sheets("Artikel").Range("B2:B1000")

Both Combo's match each other:

If ComboBox1 = A3 Then
ComBox2 shows B3 etc

ComboBox1 = Sheets("PartsDeliver").Range("D28")
ComboBox2 = Sheets("PartsDeliver").Range("D29")

It would be easier if i could use 1 Combo with 2 Columns where:
Column1 = Sheets("Artikel").Range("A2:A1000")
Column2 = Sheets("Artikel").Range("B2:B1000")

Column1 = Sheets("PartsDeliver").Range("D28")
Column2 = Sheets("PartsDeliver").Range("D29")

I've been trying to set this up but i just can't get it to work!

Anyone who could offer some help on this?

/Roger
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Here is one method to accomplish what you are looking for. Now, there are many different ways to do this, but this is just one that I have used in the past.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()
    <SPAN style="color:#00007F">Dim</SPAN> rangeArray()
    
    rangeArray = RangeToArray(Sheets("Artikel").Range("A2:B1000"))
    
    ComboBox1.columnCount = 2
    ComboBox1.Column() = rangeArray
    
    Erase rangeArray
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> RangeToArray(cellRange <SPAN style="color:#00007F">As</SPAN> Range) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> tmpArry()
    <SPAN style="color:#00007F">Dim</SPAN> currentRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, currentColumn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> columnCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, rowCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    columnCount = cellRange.Columns.Count
    rowCount = cellRange.Rows.Count
        
    <SPAN style="color:#00007F">ReDim</SPAN> tmpArry(columnCount - 1, rowCount - 1)
    
    <SPAN style="color:#00007F">For</SPAN> currentColumn = 1 <SPAN style="color:#00007F">To</SPAN> columnCount
        <SPAN style="color:#00007F">For</SPAN> currentRow = 1 <SPAN style="color:#00007F">To</SPAN> rowCount
            tmpArry(currentColumn - 1, currentRow - 1) = cellRange.Cells(currentRow, currentColumn)
        <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Next</SPAN>
    
    RangeToArray = tmpArry
    
    Erase tmpArry
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>
 
Upvote 0
Hi Tommy

Thanks for the response!

Questions:

When i select an ComboBox item list, how can i make the Combo show both Columns?

How do i get the value from Column1 to ex: A1
and Column2 to A2?

/Roger
 
Upvote 0
Answers:

1. Both columns should be showing since the ColumnCount property was set to 2. You may need to resize your ComboBox, or adjust the ColumnWidths property of the ComboBox.

2. To retrieve the value of each column...you would access the Column property. Following is an example of code to be attached to a command button.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()
    <SPAN style="color:#00007F">If</SPAN> ComboBox1.ListIndex <> -1 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">'make sure there is a selection</SPAN>
        Range("C1") = ComboBox1.Column(0)
        Range("C2") = ComboBox1.Column(1)
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Hi Again

When i open the list in the Combo i have two columns with value's. Then if i select a value in either list (by clicking), the list close and only the left Column Value shows in the Combo.

If its possible i'd like to have both columns value shown.

I've tried all changes i can think of so if you have any suggestions about this it would be great.

Everything else works like a charm... and t's so much easier when you see both columns at the same time.

Thanks a lot!

/Roger
 
Upvote 0
Like I said, you'll have to resize your ComboBox or adjust the ColumnWidths property.
 
Upvote 0
Hi Again

I tried that before my last post, both making the Combo large as...
and to adjust the ColumnWidths.

I even tried to change ColumWidths in the "UserForm_Initialize" event, but even then it only shows the left Column value in the Combo.

Did i miss something?

/Roger
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,664
Members
448,976
Latest member
sweeberry

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