Is it possible to set the RowSource of a combo box in VBA to incorporate multiple ranges?

BetterBeef

New Member
Joined
Mar 18, 2010
Messages
42
I was wondering if it is possible to set a rowsource of a combo box that I am using on a form in VBA (for Excel) using multiple ranges. For example, I would like to use the values in 'Sales Update'!A44:A51 and 'Sales Update'!A53:A63 to show up as one list in a combo box.

Is this possible or is there a way around this? I tried using Union() to bind them together, but it gave me an error message.

Any thoughts?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Sorry but the answer is no.:eek:

But I'm afraid the question is why?:)

What is it you are trying to do and why do you have your data stored/structured like this?

You could probably use other methods/properties to populate the control. eg AddItem, List etc:)
 
Upvote 0
The data is layed out like this because it is a report that has headings down the page. It is layed out this way because it is a report that is printed and this conserves paper space. I know how to write the code to populate on intialization of the form. Which event do I use and how do I get to that event to start coding?
 
Upvote 0
In design view select UserForm from the left combobox, then select Initialize from the right combobox.

You should now see this.
Code:
Private Sub UserForm_Initialize()
 
End Sub
That's where any code you want to run when the form is opened should go.:)

PS I think it might be an idea to reconsider the layout of your data - it really doesn't sound conducive to analysis/reporting/exploding squirrels.:)
 
Upvote 0
And what is the code that I use to to set the combo box (cboSalesmen) to the range(s) I want to use?
 
Upvote 0
You could try something like this.
Code:
Private Sub UserForm_Initialize()
Dim cl As Range
 
    For Each cl In Worksheets("Sales Update").Range("A44:A51")
    
        ComboBox2.AddItem cl.Value
        
    Next cl
    
    For Each cl In Worksheets("Sales Update").Range("A53:A63")
    
        ComboBox2.AddItem cl.Value
        
    Next cl
    
End Sub
That should work to populate the combobox but that combobox isn't going to be easy to work with.:)
 
Upvote 0
You can use a named range to populate a combobox but I'm pretty sure you couldn't use more than one named range - especially if they aren't contiguous.

Also as far as I can see using non-contiguous ranges is not a good idea, either to populate the control or work with the data.:)
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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