Maco that prompts password entry based on pick list values

kwp004

Board Regular
Joined
Dec 27, 2016
Messages
93
Here's what I'm trying to accomplish:
  • In cell A1 of Sheet1, I have data validation that requires the user to select from a pick list in Column B of Sheet 2.
  • I have a button that runs a macro.
  • If the user clicks the button, I want them to be prompted to enter a password based on the pick list selection, the corresponding value in Column B of Sheet 2, and an adjacent password in Column C of Sheet 2.
  • If the password is correct, the macro runs. If it's incorrect, they get an error message.
So for example:
  1. In Sheet 2, Range B1 = "Option 1". Range C1 = "Password 1"
  2. In Sheet 2, Range B2 = "Option 2" Range C2 = "Password 2".
The user goes to A1 in Sheet 1, selects "Option 2" in the pick list. When they click a button to run a the macro, they need to enter "Password 2", or else they get an error message.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If I'm reading that article correctly, I can launch a different macro based on the pick list value. So how should I apply that concept here? My goal is to run the same macro, except a different password will be required at the outset, based on the pick list value.
 
Upvote 0
I believe you should alter the approach ... Based on the pick list value, a different macro will run.

This will select the different password and the macro it runs can be the same for all. It is using a SELECT CASE statement
which makes it easier when using the DropDown select.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B2")) Is Nothing Then
    Select Case Range("B2")
        Case "Macro1": Macro1
        Case "Macro2": Macro2
        Case Else: MsgBox "Macro not available"
    End Select
End If
End Sub
 
Upvote 0
Have your button call this macro
VBA Code:
Sub myMacro()
    Dim uiPassword As String
    Dim currentPassword As Variant
    
    currentPassword = Application.VLookup(Sheet1.Range("A1").Value, Sheet2.Range("B:C"), 2, False)
    
    If IsError(currentPassword) Then
        Rem Sheet1!A1 does not have anything selected.
        Exit Sub
    End If
    
    uiPassword = Application.InputBox("Enter a case insensitive password", Type:=2)
    If uiPassword = "False" Then Exit Sub: Rem cancel pressed
    
    If LCase(uiPassword) <> LCase(currentPassword) Then
        MsgBox "wrong password"
        Exit Sub
    End If
    
    ' your code
    
End Sub
 
Upvote 0
I'm hoping mikerickson can help me again (or that someone else can apply the concept above).

Basically, the solution mikerickson posted above still works, but I need help with the macro that will run after it (i.e. after ' your code in mikerickson's solution). I need to unhide very hidden worksheets based on the password that was entered. So using the example above, if "Password 1" is entered, it should open "Sheet3". If "Password 2" is entered, it should open "Sheet4". Is it possible to do that by using another vlookup to Sheet 2? So for example in Sheet 2:

Column BColumn CColumn D
Option 1Password 1Sheet 3
Option 2Password 2Sheet 4

Also, I'd like to make the sheet that unhides become Very Hidden again if the user navigates to another sheet, or closes out the file.
 
Upvote 0
VBA Code:
Select Case LCase(uiPassword)
    Case "password 1"
        ThisWorkbook.Sheets("Sheet1").Visible = xlSheetVisible
    Case "password 2"
        ThisWorkbook.Sheets("Sheet2").Visible = xlSheetVisible
End Select
 
Upvote 0

Forum statistics

Threads
1,215,329
Messages
6,124,301
Members
449,149
Latest member
mwdbActuary

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