Drop down box using sheet names

Mattyads2011

New Member
Joined
Jun 16, 2011
Messages
23
Hi All.

I need to create a button that displays a drop down box that allows a user to jump to a specific worksheet in an excel document.

The main sticking point is that there may be new sheets added with various names throughout the year that, once created, will need to form part of the drop down menu.

Can this even be done?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Create a userform (in the vba screen, use Alt + F11, then Insert UserForm) and add a combo box and name it something like cboSheetNames

Double click the form and from the code screen at the top select on the right Initialize then add this code.

Dim wksht As Worksheet
For Each wksht In Worksheets
Me.cboSheetNames.AddItem wksht.Name
Next

This will give you a list in the drop down of all the sheet names and when you add more sheets it will then show them in the list the next time the form is run.

To run the form you would Insert a Module sheet and then place in a macro

Sub OpenSampleForm()
frmSample.Show
End Sub

Add a command button in the form to go to the worksheet. Double click the button and add this code

Private Sub cmdOK_Click()
Sheets(Me.cboSheetNames.Value).Select
Unload me
End Sub
 
Upvote 0
Right-click any arrow to the left side of sheet tabs.
 
Upvote 0
Thanks guys.

Once the workbook has been finished the sheet tabs wont be displayed as I do not want everyone using it to have access to all sheets so I am using a series of menu's for users.

Thanks very much for the code, I havent tried it yet, but can I ask, if i wanted to exclude a few sheets from the selection could I do that? If so how please?
 
Upvote 0
You could get all the sheet names into a worksheet and then run some code to delete the names you don't want to appear then fill the combo box with the list.

How does that sound?

Please give some idea of sheet names you don't want to include.

If OK post back and a solution will be provided.
 
Upvote 0
Hi Trevor.

I have some code that a user can 'add' a new page any time they want (actually it is just copying a sheet and the user can rename it to refer to the member of staff)

I then need this drop down box to allow that user to go back to the sheet they created in order to edit the information on there whenever they need to, I thought having a drop down menu would probably be the most user friendly way, as i have said before i cannot allow users access to the sheet tabs for various reasons.

Sheet names I do not want to appear in the list are, but not limited to, are; Home Screen, Database, Talent Charts

I hope this makes sense and thank you for your time.
 
Upvote 0
Ok this will need more code. Create the form with the objects as mentioned, then add an extra sheet called, Sheet Names. Next behind the form on the initialise area you will need to add the following code

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()<br><SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet<br><SPAN style="color:#00007F">Dim</SPAN> S <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">For</SPAN> S = 1 <SPAN style="color:#00007F">To</SPAN> Worksheets.Count<br><SPAN style="color:#00007F">With</SPAN> Worksheets("Sheet Names")<br><SPAN style="color:#00007F">Set</SPAN> ws = Worksheets(S)<br>.Cells(S, 1).Value = ws.Name<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">Next</SPAN> S<br><br><SPAN style="color:#00007F">Dim</SPAN> calcmode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> ViewMode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> myStrings <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> FoundCell <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> I <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> myRng <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> sh <SPAN style="color:#00007F">As</SPAN> Worksheet<br><br>    <SPAN style="color:#00007F">With</SPAN> Application<br>        calcmode = .Calculation<br>        .Calculation = xlCalculationManual<br>        .ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br>    <SPAN style="color:#007F00">'We use the ActiveSheet but you can also use Sheets("MySheet")</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> sh = ActiveSheet<br>    <br><br>    <SPAN style="color:#007F00">'We search in column A in this example</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> myRng = sh.Range("A:A")<br><br>    <SPAN style="color:#007F00">'Add more search strings if you need</SPAN><br>    myStrings = Array("Home Screen", "Database", "Talent Charts", "Sheet Names")<br><SPAN style="color:#007F00">'Home Screen, Database, Talent Charts</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> sh<br><br>        <SPAN style="color:#007F00">'We select the sheet so we can change the window view</SPAN><br>        .Select<br><br>        <SPAN style="color:#007F00">'If you are in Page Break Preview Or Page Layout view go</SPAN><br>        <SPAN style="color:#007F00">'back to normal view, we do this for speed</SPAN><br>        ViewMode = ActiveWindow.View<br>        ActiveWindow.View = xlNormalView<br><br>        <SPAN style="color:#007F00">'Turn off Page Breaks, we do this for speed</SPAN><br>        .DisplayPageBreaks = <SPAN style="color:#00007F">False</SPAN><br><br>        <SPAN style="color:#007F00">'We will search the values in MyRng in this example</SPAN><br>        <SPAN style="color:#00007F">With</SPAN> myRng<br><br>            <SPAN style="color:#00007F">For</SPAN> I = <SPAN style="color:#00007F">LBound</SPAN>(myStrings) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(myStrings)<br>                <SPAN style="color:#00007F">Do</SPAN><br>                    <SPAN style="color:#00007F">Set</SPAN> FoundCell = myRng.Find(What:=myStrings(I), _<br>                                               After:=.Cells(.Cells.Count), _<br>                                               LookIn:=xlFormulas, _<br>                                               LookAt:=xlWhole, _<br>                                               SearchOrder:=xlByRows, _<br>                                               SearchDirection:=xlNext, _<br>                                               MatchCase:=False)<br>                    <SPAN style="color:#007F00">'Use xlPart If you want to search in a part of the FoundCell</SPAN><br>                    <SPAN style="color:#007F00">'If you use LookIn:=xlValues it will also delete rows with a</SPAN><br>                    <SPAN style="color:#007F00">'formula that evaluates to "Ron"</SPAN><br>                    <SPAN style="color:#00007F">If</SPAN> FoundCell <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>                        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Do</SPAN><br>                    <SPAN style="color:#00007F">Else</SPAN><br>                        FoundCell.EntireRow.Delete<br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">Loop</SPAN><br>            <SPAN style="color:#00007F">Next</SPAN> I<br><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br>    ActiveWindow.View = ViewMode<br>    <SPAN style="color:#00007F">With</SPAN> Application<br>        .ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>        .Calculation = calcmode<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br>Range("A1").Select<br><SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">Until</SPAN> ActiveCell.Value = ""<br>    Me.cboSheetNames.AddItem ActiveCell.Value<br>    <br>    ActiveCell.Offset(1, 0).Select<br>    <br><SPAN style="color:#00007F">Loop</SPAN><br>Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Thanks Trevor.

Something has come up at work so may not get to try this today now but it certainly looks good. I might have 1 or 2 questions tomorrow if that is ok?


Thanks again.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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