Possible to call objects with a variable in VBA

lukeshuttlewood

Board Regular
Joined
Jul 27, 2004
Messages
90
Hi,

I have a list of combo boxes named like this on a form:

combobox1
combobox2
combobox3 etc


Since there is so many of them, is it possible to call them in a way such as below?

I cannot get the syntax right......

Code:
    x = 10
    Do While x > 0
        ComboBox(x).AddItem "TRUE"
        ComboBox(x).AddItem "FALSE"
        x = x - 1
    Loop


Thanks
Luke
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Short answer: Sort of.

Long answer: Class Modules can probably do what you want.

Alternative Answer:

If you name your objects properly, then you can loop through all objects of a certain type with a certain name to accomplish the same sort of thing.
 
Upvote 0
Assuming the control is part of a userform and the code is in the userform's code module...Also, untested...
Code:
    x = 10
    Do While x > 0
        Me.Controls("ComboBox" & X).AddItem "TRUE"
        ...
        x = x - 1
    Loop
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,245
Members
448,952
Latest member
kjurney

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