ActiveX ComboBox To Automatically Update with Worksheet Names

MelindaMartin

New Member
Joined
Dec 19, 2016
Messages
6
I have a combox box that allows me to select a worksheet name from a drop down list and then it takes me to that worksheet. That part is awesome.

It works in conjunction with a command button. The command button generates the combobox with the worksheet names. This part is not awesome.

My goal is to have the combobox automatically update with the workbook's current worksheets and to not have a command button at all--just to have the combobox.

The combobox code is:

Private Sub ComboBox1_Change()
Dim ws As Worksheet
On Error Resume Next
Set ws = ThisWorkbook.Sheets(ComboBox1.Value)
ws.Select


On Error GoTo 0
End Sub

The command box code is:

Private Sub CommandButton1_Click()
Dim n As Long
For n = 1 To ActiveWorkbook.Sheets.Count
ComboBox1.AddItem ActiveWorkbook.Sheets(n).Name
Next n
End Sub

How do I get the combobox to automatically update with any new and/or deleted worksheet names?

TIA
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this:

Code:
Private Sub ComboBox1_Click()
Sheets(ComboBox1.Value).Activate
End Sub
Private Sub ComboBox1_GotFocus()
Dim i As Long
ComboBox1.Clear
For i = 1 To Sheets.Count
ComboBox1.AddItem Sheets(i).Name
Next
End Sub

OK
 
Upvote 0
Worked Great! And in the short time I was waiting, i also got it to work in a user form via a different method.

Thanks so much! I didn't want to have to go to a form, and this is perfect.

Try this:

Code:
Private Sub ComboBox1_Click()
Sheets(ComboBox1.Value).Activate
End Sub
Private Sub ComboBox1_GotFocus()
Dim i As Long
ComboBox1.Clear
For i = 1 To Sheets.Count
ComboBox1.AddItem Sheets(i).Name
Next
End Sub


OK
 
Upvote 0
Glad I was able to help you. Come back here to Mr. Excel next time you need additional assistance.
Worked Great! And in the short time I was waiting, i also got it to work in a user form via a different method.

Thanks so much! I didn't want to have to go to a form, and this is perfect.
 
Upvote 0
I spend my day helping people in the self-publishing industry and run a forum of my own, so it's nice to get to play the damsel in distress for a change, lol. Knowledge is power, and I appreciate you sharing your power with me.
 
Upvote 0
I like helping others and learning more every day myself. Excel can do wonders.
I spend my day helping people in the self-publishing industry and run a forum of my own, so it's nice to get to play the damsel in distress for a change, lol. Knowledge is power, and I appreciate you sharing your power with me.
 
Upvote 0
I see what your referring to. Not sure why that is happening.
Try these scripts:

To load the combobox with values you will need to activate the sheet.
Activate means you need to go to another sheet and then come back to this sheet. But only once unless you add more sheets then you will have to activate the sheet with the combobox again.


Code:
Private Sub ComboBox1_Click()
Sheets(ComboBox1.Value).Activate
End Sub
Private Sub Worksheet_Activate()
Dim i As Long
ComboBox1.Clear
    For i = 1 To Sheets.Count
        ComboBox1.AddItem Sheets(i).Name
    Next
End Sub

OK
 
Upvote 0
Or try this:

To load the combobox with new sheet names doubleclick on the combobox

Code:
Private Sub ComboBox1_Click()
Sheets(ComboBox1.Value).Activate
End Sub
Private Sub ComboBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Long
ComboBox1.Clear
    For i = 1 To Sheets.Count
        ComboBox1.AddItem Sheets(i).Name
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,045
Messages
6,128,477
Members
449,455
Latest member
jesski

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