VBA syntax help needed!

cedricthecat

Active Member
Joined
May 17, 2007
Messages
460
Hi folks

If I use the first code below, it works without issue.

If I change the line in red, then it gives an error.

In the first code, the password is a choice of the value in cell Q2 on sheet "Words" and the password "password". In the second it's a choice of the values of Q2 or Q3 - except this doesn't work and I'm not sure why!

All help appreciated!

Code:
Sub ShowWords()
ActiveWorkbook.Unprotect Password:="pass"
    Application.EnableEvents = False
      PassEntry.Show
        [COLOR=red]If strPass <> Sheets("Words").Range("Q2") And strPass <> "password" Then[/COLOR]
                MsgBox "Incorrect Password"
        ThisWorkbook.Activate
        Application.EnableEvents = True
    Else
        Sheets("Words").Visible = True
        ThisWorkbook.Activate
        Sheets("Words").Activate
 
    End If
        Application.EnableEvents = True
        Call SheetNames
End Sub

Non working:-

Code:
Sub ShowWords()
ActiveWorkbook.Unprotect Password:="pass"
    Application.EnableEvents = False
      PassEntry.Show
       [COLOR=red]If strPass <> Sheets("Words").Range("Q2") And strPass <> Sheets("Words").Range("Q3")Then[/COLOR]
                MsgBox "Incorrect Password"
        ThisWorkbook.Activate
        Application.EnableEvents = True
    Else
        Sheets("Words").Visible = True
        ThisWorkbook.Activate
        Sheets("Words").Activate
 
    End If
        Application.EnableEvents = True
        Call SheetNames
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
What error does it give? Also, what is strPass? Where is it defined?
 
Upvote 0
Sorry, strPass is defined in a Userform

Even more sorry, the darned thing works now! It wasn't working at work, so I came home and posted for help and now it does!

Not sure what I got wrong before!
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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