PDA

View Full Version : Excel 2007 error with code for creating passwords



CMSS
08-04-2010, 06:43 AM
I am making a macro that when a workbook opens, an inputbox runs that asks for a password. Valid passwords are certain names, and what name you enter determines what type of access you have in the document (i.e., 'bob' allows data to be entered, 'mary' hides certain rows and columns, etc.). I do this by having on another worksheet in the workbook the valid names in one column and next to them code words that determine the permissions allowed for that name. When a name is entered the code searches for the name, and if it is valid it sets the corresponding code word located next to it as a variable. I want an error message to appear if a name that is not on the approved list is entered, the default entry in the inputbox is left in the user input line, or if the user input line is blank, and cycle back to the original inputbox. Also, I only want the user to have three chances at entering a valid name before the workbook closes automatically. In addition, if the user hits 'cancel' on the inputbox, I want the workbook to close.

Here is my code thus far:



Dim name As String
Dim permission As String
Dim i As Long
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Sheet1").Unprotect Password:="c"
Sheets("Sheet1").Visible = xlSheetVeryHidden
For i = 1 To 3
name = Application.InputBox("Please Enter Name", "Login", "Enter Name Here", Type:=2)
Sheets("Sheet3").Select
If permission = Range("A1:A4").Find(name, LookIn:=xlValues, LookAt:=xlWhole).Offset(0, 1) Then
Select Case permission
Case "admin": Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Range("A1:M23").Locked = False
Range("A1").Select
ActiveSheet.Protect Password:="c"
ActiveSheet.EnableSelection = xlUnlockedCells
Case "finance": Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Columns("A:B").Hidden = True
Rows("5:10").Hidden = True
Range("C1").Select
ActiveSheet.Protect Password:="c"
Case "superuser": Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Range("F1:F3, F5:F23").Locked = False
Columns("B").Hidden = True
Rows("4").Hidden = True
Range("A1").Select
ActiveSheet.Protect Password:="c"
ActiveSheet.EnableSelection = xlUnlockedCells
Case "user": Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Columns("A:D").Hidden = True
Rows("20:23").Hidden = True
Range("E1").Select
ActiveSheet.Protect Password:="c"
End Select
Exit Sub
Else
MsgBox ("Invalid Name Entered!")
End If
Next i
Application.Quit


I either get an error message saying I have an 'End If' without an 'If', or a 'Next' without a 'For', or an 'Else' without an 'If'.

Bob Phillips
08-04-2010, 07:53 AM
Dim name As String
Dim permission As String
Dim i As Long
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Sheet1").Unprotect Password:="c"
Sheets("Sheet1").Visible = xlSheetVeryHidden
For i = 1 To 3
name = Application.InputBox("Please Enter Name", "Login", "Enter Name Here", Type:=2)
Sheets("Sheet3").Select
If IsError(aplication.Match(name, Range("A1:A4"), 0)) Then

MsgBox "No such name"
Else

permission = Range("A1:A4").Find(name, LookIn:=xlValues, LookAt:=xlWhole).Offset(0, 1)
Select Case permission
Case "admin": Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Range("A1:M23").Locked = False
Range("A1").Select
ActiveSheet.Protect Password:="c"
ActiveSheet.EnableSelection = xlUnlockedCells
Case "finance": Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Columns("A:B").Hidden = True
Rows("5:10").Hidden = True
Range("C1").Select
ActiveSheet.Protect Password:="c"
Case "superuser": Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Range("F1:F3, F5:F23").Locked = False
Columns("B").Hidden = True
Rows("4").Hidden = True
Range("A1").Select
ActiveSheet.Protect Password:="c"
ActiveSheet.EnableSelection = xlUnlockedCells
Case "user": Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Columns("A:D").Hidden = True
Rows("20:23").Hidden = True
Range("E1").Select
ActiveSheet.Protect Password:="c"
End Select
End If
Next i
Application.Quit

CMSS
08-04-2010, 08:52 AM
Thanks for the help, I fixed the problem!