Set Variable to ListBox in VBA?

JeffK627

Active Member
Joined
Jun 22, 2005
Messages
313
I'm trying to set an object variable to a listbox (not the selected item, the listbox itself). I've tried declaring the variable as a ListBox and as a Control, but neither works. For example, the following returns a type mismatch error:

Code:
Dim cListBox As ListBox
Set cListBox = Sheet1.lstProduct

I get the same error if I dim cListBox as a Control. Sheet1.lstProduct exists and comes up in autocomplete, so it isn't that the control isn't there. Does anyone know how to do this? Is it even possible?

The reason I'm trying to do this is that I have several similar list boxes that are used to set values in a SQL query and I'd like to have one Function for all of them instead of one for each, since the code is almost identical for each one.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Well, I solved it by setting the type to Object. Not crazy about the generic option, but it was what worked.
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,508
Members
452,918
Latest member
Davion615

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