ComboBox with Sheet names

Nalani

Well-known Member
Joined
Apr 10, 2009
Messages
1,047
I'm using the below code to populate my ComboBox on a Userform but running into a little problem.

Code:
Private Sub UserForm_Initialize()
Dim ws As Integer
For ws = 1 To Sheets.Count
ComboBox1.AddItem Sheets(ws).Name
Next
End Sub

It does not list the sheets within the CB. But if I click on the Scroll Bar, all sheets are listed.

From there I can click on one of them and then it will appear within the CB.

If I click on the Scroll Bar again and choose another sheet, the first one is replaced by the second choice.

Don't know what's going on here. :confused:
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
What do you expect to happen that isn't taking place?

I created a user form with combobox1 and using the attached code it is populated with the sheets in the workbook.

If I click on the drop down arrow I see all the sheets and then I can select one or anyone.
 
Upvote 0
Try this variation of your code to load the combobox with the sheet names and select the first listitem:

Code:
Private Sub UserForm_Initialize()
Dim ws As Worksheet
'Load ComboBox1 with all worksheet names
'and select the first item
With ComboBox1
    .Clear
    For Each ws In ThisWorkbook.Worksheets
        .AddItem ws.Name
    Next ws
[B]   .ListIndex = 0[/B]
End With
End Sub

Is that something you can work with?
 
Upvote 0
What I am expecting is.

My ComboBox is large enough to show all Worksheets, only 5 at the moment. I would like to have all ws to show in the combo box.

Never mind, as I was typing this out I realized I need a ListBox. daaa

One of those days ! !

Thanks to both of you guys for at least trying to set my brain straight. (sorry)
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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