VBA Macro Coding which will take you to the Excel sheet you select from a drop down list on Sheet1.

Galapagos15

Board Regular
Joined
Sep 16, 2015
Messages
100

  1. An excelWorkbook has four sheets which are named Sheet1, HR, IT and Finance.
  2. Sheet1 has adrop down list in cell A1 which lists the names of the other three sheets whichare the company departments HR, IT and Finance. <o:p></o:p>
  3. I want theuser to open the Excel Workbook and at first only have access to Sheet1 so the otherthree sheets will be locked. <o:p></o:p>
  4. On Sheet1 theywill select their department name from the drop down list in cell A1.<o:p></o:p>
  5. Once theyselect their department from the drop down list on Sheet1 the macro will unlockthat sheet for the user and take the user to that sheet so they can enter information which would be eitherthe HR, IT or Finance sheet. The other two department sheets will remain locked.
<o:p> </o:p>
Thank you inadvance!<o:p></o:p>
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Place this macro in the code module for ThisWorkbook:
Code:
Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "Sheet1" Then
            ws.Visible = False
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub

Place this macro in the code module for Sheet1:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "Sheet1" Then
            ws.Visible = False
        End If
    Next ws
    Sheets(Target.Value).Visible = True
    Sheets(Target.Value).Select
    Application.ScreenUpdating = False
End Sub
The first macro will ensure that only Sheet1 will be visible when the workbook is opened. The second macro will activate the sheet selected in the drop down in A1.
 
Upvote 0
Thank you so much! Is there a way that the Macro will still make all Sheets visible but only unlock the one that they select from the drop down list?
 
Upvote 0
Replace the previous 2 macros with these:
Code:
Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "Sheet1" Then
            ws.Unprotect
            ws.Cells.Locked = True
            ws.Protect
            ws.EnableSelection = xlUnlockedCells
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "Sheet1" Then
            ws.Unprotect
            ws.Cells.Locked = True
            ws.Protect
            ws.EnableSelection = xlUnlockedCells
        End If
    Next ws
    Sheets(Target.Value).Unprotect
    Sheets(Target.Value).Select
    Application.ScreenUpdating = False
End Sub
You should start by protecting all the sheets except Sheet1. I have not used a password. If you want to protect the sheets with a password, please let me know and I will modify the macros.
 
Last edited:
Upvote 0
Thank you so much for your help. Unfortunately, when I revise the coding and open the file it must be protecting Sheet1 because the drop down list in Cell A1 no longer works.
 
Upvote 0
This line of code in both macros:
Code:
If ws.Name <> "Sheet1" Then
excludes Sheet1 from being locked so I'm not sure why you are having that problem. It is always easier to help and test possible solutions if we could work with your actual file. Perhaps you could upload a copy of your file to a free site such as www.box.com. or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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