Excel VBA Help Needed Please!

evil_moses

New Member
Joined
Oct 27, 2008
Messages
39
Hi all,

I'm new to this forum so be gentle please! Nice to meet you all anyway.

I am trying (very unsucessfully) to write a macro that does the following:

Ask user for a password
If password is correct then hide column J and protect sheet
If ok is selected then exit macro
If password is wrong then display error message.

I am having trouble doing two things.

1. When you press cancel, the error message is displayed instead of just exiting the macro
2. When a wrong password is entered, I want to display the error message and then loop back to the password entry box again.

If you could have a look at my code and help me I would really appreciate it!

Sub Show_Admin_Salary()
'
' Show_Admin_Salary Macro
' Macro recorded 27/10/2008 by Marcus Ponting
'

Dim pw
Dim vbCancel: vbCancel = 2
pw = Application.InputBox("Please enter Password:")
If pw = vbCancel Then
Exit Sub
End If
If pw = "amber1" Then
Sheets("Admin").Select
Dim shtAnySheet As Worksheet
Dim sPassword As String
sPassword = "amber1"
Set shtAnySheet = Worksheets("Admin")
shtAnySheet.Unprotect sPassword
Columns("I:K").Select
Selection.EntireColumn.Hidden = False
Range("A1").Select
Else
MsgBox "Incorrect Password"
End If
End Sub


Thanks a lot

Marcus
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I have a question regarding what you're asking for. If the password is correct, then you want to hide column J and protect the sheet. Does this mean that the sheet is unprotected by default? If so, then if the user doesn't accept macros then they will have full access to the sheet. You sure that's the way you want to handle it?
 
Upvote 0
Code:
Sub Show_Admin_Salary()
'
' Show_Admin_Salary Macro
' Macro recorded 27/10/2008 by Marcus Ponting
'
Dim pw
    pw = Application.InputBox("Please enter Password:")
    If pw = False Then Exit Sub
    If pw = "amber1" Then
        Dim shtAnySheet As Worksheet
        Dim sPassword As String
        sPassword = "amber1"
        Set shtAnySheet = Worksheets("Admin")
        With shtAnySheet
            .Unprotect sPassword
            .Columns("I:K").Hidden = False
            .Columns("J").Hidden = True
            .Protect sPassword
        End With
    Else
        MsgBox "Incorrect Password"
    End If
End Sub
 
Upvote 0
Hi Bowlinbd,

That'll teach to fire off a thread without reading it first!

What I meant to say was this

Ask user for a password
If password is correct then unprotect sheet and unhide column
If cancel is selected then exit macro
If password is wrong then display error message.

I already have a macro that works that does the reverse, ie hide columns and protect sheet, but this macro doesn't need a password, which is where I am having trouble in this macro.

By default, the column is hidden and the sheet is protected.

Thanks for your help

Marcus
 
Upvote 0
To test if the user presses Cancel on an Input box:

Code:
Dim UserInput As String

UserInput = InputBox("enter")

If StrPtr(UserInput) = 0 Then
    MsgBox "Cancel pressed"
ElseIf UserInput = vbNullString Then
    MsgBox "Nothing entered in box. OK pressed"
Else
    MsgBox "User entered: " & UserInput
End If
 
Upvote 0
An adaptation of xld's code. This should incorporate the loops.

Code:
Sub Show_Admin_Salary()
    Dim shtAnySheet As Worksheet
    Dim sPassword As String
    Dim pw
        
    sPassword = "amber1"
    pw = Application.InputBox("Please enter Password:")

    If pw = sPassword Then
        Set shtAnySheet = Worksheets("Admin")
        With shtAnySheet
            .Unprotect sPassword
            .Columns("J").Hidden = False
            .Protect sPassword
        End With
        Exit Sub
        
    ElseIf pw = False Then
        MsgBox "Error Message"
        Show_Admin_Salary
        
    ElseIf pw <> sPassword Then
        MsgBox "Incorrect Password"
        Show_Admin_Salary
        
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,210
Members
448,554
Latest member
Gleisner2

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