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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
What error does it give? Also, what is strPass? Where is it defined?
 

cedricthecat

Active Member
Joined
May 17, 2007
Messages
460
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!
 

Forum statistics

Threads
1,148,160
Messages
5,745,123
Members
423,927
Latest member
Pra56

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
Top