Excel 2007 error with code for creating passwords

CMSS

New Member
Joined
Jul 29, 2010
Messages
18
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:

Code:
    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'.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I don't know why it is working for you, but my code is now working to an extent. I no longer get the previous error messages, but if a correct name is entered into the inputbox, it registers as an incorrect entry, and opens the error message box, and after three 'correct' names entered, it closes. It will also close after three invalid attempts.
 
Upvote 0
I didn't say it was working, just that it compiled. This isn't right:

Rich (BB code):
If permission = Range("A1:A4").Find(name, LookIn:=xlValues, LookAt:=xlWhole).Offset(0, 1) Then

Change it to:

Rich (BB code):
        Set permission = Range("A1:A4").Find(name, LookIn:=xlValues, LookAt:=xlWhole).Offset(0, 1)
        If Not permission Is Nothing Then

and change the permission declaration to:

Rich (BB code):
    Dim permission As Range
 
Upvote 0

Forum statistics

Threads
1,217,394
Messages
6,136,355
Members
450,006
Latest member
DaveLlew

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