Changing a ComboBox reference using a Variable

Muggles

New Member
Joined
Jul 26, 2022
Messages
3
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
Hey there,

I am trying to create a code where I can do do the following With command for a ComboBox where it uses a variable to change the ComboBox name.

Currently it is:


With Sheet1.ComboBox1
.AddItem Sheet1.Range("B" & i).Value
End With

But I would like it to be something like

Dim Variable

Variable = 1

With Sheet1."ComboBox" & Variable
.AddItem Sheet1.Range("B" & i).Value
End With

Is there a way to easily do this?

Thanks so much in advance for your help!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
@Muggles
Welcome to MrExcel
Example:
VBA Code:
i = 1
MsgBox Sheets("Sheet1").OLEObjects("combobox" & i).Object.Value
 
Upvote 0
Thank you so much for your quick reply!

When i've gone to do this I get an error:

Run-time error '9"
Subscript out of range

Any ideas on what i'm doing wrong here?
 
Upvote 0
Are you using Active x control or Form control?
 
Upvote 0
If it is an Active x control then something like this:
VBA Code:
Sub try()
Dim i As Long
Dim n
n = 1
i = 4
With Sheet1.OLEObjects("combobox" & n).Object
    .AddItem Sheet1.Range("B" & i).Value
End With
End Sub
 
Upvote 0
Thanks that got it, it was the fact it was Active X.

Appreciate your help!
 
Upvote 0

Forum statistics

Threads
1,223,941
Messages
6,175,537
Members
452,652
Latest member
eduedu

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