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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

bmacd615

New Member
Joined
Mar 28, 2009
Messages
8
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

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
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

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
ADVERTISEMENT
Jim

What would you be using 1-8 for?
 
Upvote 0

bmacd615

New Member
Joined
Mar 28, 2009
Messages
8
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

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,875
Office Version
  1. 2019
  2. 2016
  3. 2010
Platform
  1. Windows
ADVERTISEMENT
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

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,350
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

bmacd615

New Member
Joined
Mar 28, 2009
Messages
8
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,196,021
Messages
6,012,904
Members
441,740
Latest member
Latrs

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
Top