Code For COMBOBOX

LearnMeExcel

Well-known Member
Joined
Aug 11, 2009
Messages
746
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi
i add combobox in my sheets
i want to add the sheets name to Combobox
i wrote this code
Code:
Private Sub ComboBox1_GotFocus()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Main" Then
        ComboBox1.AddItem ws.Name
    End If
Next ws
End Sub
but the problem every time i click the combobox the sheets name repeat,for example
first time the result
Sheet 1
Sheet 2
Sheet 3
the second time
Sheet 1
Sheet 2
Sheet 3
Sheet 1
Sheet 2
Sheet 3
how i can solve it
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try

Code:
Private Sub ComboBox1_GotFocus()
Dim ws As Worksheet
Dim i As Long
For i = 1 To ComboBox1.ListCount
    ComboBox1.RemoveItem 0
Next i
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Main" Then
        ComboBox1.AddItem ws.Name
    End If
Next ws
End Sub
 
Upvote 0
Thanx Peter

Dim i As Long
For i = 1 To ComboBox1.ListCount
ComboBox1.RemoveItem 0
Next i

why you put 0


i have another Point
i want to add HyperLink for every sheets in my workbook to back to main
i test this code
Code:
Sub AddHyperLink()
Dim ws As Worksheet
For Each ws In Sheets
    If ws.Name <> "Main" Then
        ws.Hyperlinks.Add Anchor:=Range("a1"), Address:="", _
        SubAddress:="Main!B2", TextToDisplay:="Back To Main"
    End If
Next ws
End Sub
it just add HyperLink to Main
should i activate every sheet in sheets before add Hyperlink
 
Upvote 0
0 means row 1 in the combobox entries.

Try a new thread for your other question. I am about to go out for the day.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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