Rowsource reference for Combobox

bmacd615

New Member
Joined
Mar 28, 2009
Messages
8
This code is in my Private Sub UserForm_Initialize().
I need to specify the range AND SHEET in the following code. The range portion of this code works. This routine will get the correct RANGE, but it does not select the correct sheet. It uses the range of whichever sheet is active when I run the form. How can I make the code get the right SHEET.

For i = 1 To 8
Me.Controls("Combobox" & i).RowSource = Worksheets("GAB").Range("a1:a5").Address
Next i
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I guess I found a simple solution. I used Worksheets("GAB").Activate to activate the sheet before the For Next routine, and then activated the "home page" work sheet after the For Next routine.

Application.ScreenUpdating = False
Worksheets("GAB").Activate
For i = 1 To 8
Me.Controls("Combobox" & i).RowSource = Worksheets("GAB").Range("a1:a5").Address
Next i
Worksheets("Input&Output").Activate
Application.ScreenUpdating = True

This works. Is there a better way?
 
Upvote 0
Why not just include the field in the RowSource?
Code:
Private Sub UserForm_Initialize()
Dim I As Long
    
    Application.ScreenUpdating = False
    For I = 1 To 8
        Me.Controls("Combobox" & I).RowSource = "GAB!A1:A5"
    Next I
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Jim

What would you be using 1-8 for?
 
Upvote 0
I am using the For next loop because I have 8 boxes that I want the user to fill with eight items from a list that occupies the a1:a5 range (when completed there will be about 30 items A1:a30 to select from and fill the boxes).
 
Upvote 0
Comboboxes also could be populated without RowSource property:
Rich (BB code):
<font face=Courier New>
Private Sub UserForm_Initialize()
  Dim i As Long, arr()
  arr = Worksheets("GAB").Range("A1:A5").Value
  For i = 1 To 8
    Me.Controls("Combobox" & i).List = arr
  Next i
End Sub</FONT>
 
Upvote 0
Row Source isn't supported on my machine, but you might try
Code:
Application.ScreenUpdating = False
'Worksheets("GAB").Activate
For i = 1 To 8
    Me.Controls("Combobox" & i).RowSource = Worksheets("GAB").Range("a1:a5").Address(, , , True)
Next i
Worksheets("Input&Output").Activate
Application.ScreenUpdating = True
 
Upvote 0
All of the replies work.
Noire and Jim May's suggestions of

Me.Controls("Combobox" & I).RowSource = "GAB!A1:A5"

works fine now, but that was a variation I tried earlier and would give an error. But it works now and is simple!

ZVP use of an array variable works simply also.

Mikerickson's works fine but I need to understand a little more about the address parameters.

Thanks everyone. This forum is excellent!
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,431
Members
448,961
Latest member
nzskater

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