combo box.

dcmb

Board Regular
Joined
Feb 3, 2009
Messages
116
I googled but couldn't find an answer.

I'm still pretty new to VBA. And I want to use a combo box / list box to show the items in a column which I can select.

For example:

HTML:
         A
1     apple
2     orange
3     apple
4     banana

I want the combo box / list box to show the list of items in column A. And say, I select orange on the combo box / list box then I want to use the selected data for filtering.

All suggestions and ideas are welcomed.

Thanks in advance.
 
Thank you very much Michael. The alternative codes work great too.

I moved the ".Clear" up in line with "With Worksheets("Sheet1").OLEObjects("cbb1").Object" the code will fail.

i.e. instead of:

Code:
With Worksheets("Sheet1").OLEObjects("cbb1").Object
    .Clear

I tried:

Code:
With Worksheets("Sheet1").OLEObjects("cbb1").Object.Clear

Any idea what's wrong? And what's the ".Clear" for?

And for

Code:
For Each cell In Sheets("Sheet2").Range("I1:I" & lr)
            .AddItem cell.Value
        Next cell

Are we telling VBA to add the value of the cell into the combox box for every cell from in column I? And it will do so until the last row with data in column I because we assigned it to the variable "lr" which counts the number of rows that has something in them?
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The .Clear can't go on the same line...it's not an object ...it's an action.
what it's doing is telling Excel to clear that ComboBox of ALL entries, so it can start fresh each time.
Otherwise, if you added 1 more item to the list, it would add it into the combobox, but would duplicate all the other items as well.
Are we telling VBA to add the value of the cell into the combox box for every cell from in column I? ..... YES
And it will do so until the last row with data in column I because we assigned it to the variable "lr" ..... YES
which counts the number of rows that has something in them? YES
 
Upvote 0
The .Clear can't go on the same line...it's not an object ...it's an action.
what it's doing is telling Excel to clear that ComboBox of ALL entries, so it can start fresh each time.
Otherwise, if you added 1 more item to the list, it would add it into the combobox, but would duplicate all the other items as well.

Many thanks Michael.
 
Upvote 0

Forum statistics

Threads
1,215,421
Messages
6,124,806
Members
449,191
Latest member
rscraig11

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