Need your help badly
I have code that is assigned to "button" to delete the current sheet. Sheet and workbook are password protected. I am able to use the button to delete the sheet but the problem, its not protecting the workbook back. Please suggest.
Sub Deletetab()
Application.DisplayAlerts = False
If MsgBox("Deleting Current Sheet, Data entered on this sheet will be lost", vbOKCancel) = vbOK Then
ThisWorkbook.Unprotect Password:="xyz"
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
ThisWorkbook.Protect Password:="xyz"
Application.DisplayAlerts = True
Else: Exit Sub
End If
Application.DisplayAlerts = True
ThisWorkbook.Protect Password:="xyz"
End Sub
Please help.
You have ThisWorkbook.Protect Password:="xyz" twice in your code.
Please try the modified code below (tested and ran ok on my Excel):
Sub Deletetab()
Application.DisplayAlerts = False
If MsgBox("Deleting Current Sheet, Data entered on this sheet will be lost", vbOKCancel) = vbOK Then
ThisWorkbook.Unprotect Password:="xyz"
ActiveWindow.SelectedSheets.Delete
ThisWorkbook.Protect Password:="xyz"
Application.DisplayAlerts = True
End If
Application.DisplayAlerts = True
End Sub
Related
I'm trying to find a way to prevent copy/cut-pasting in excel cells. I disabled copy/pasting of cells however found a flaw that if a user copies the actual value of the cell he's able to paste thus(In only 1 cell not a range of cells) making the disabled copy/paste useless.
I used the following line of code from another question:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.CutCopyMode = False
Dim UndoList As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error GoTo ErrExit
UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(UndoList, 5) = "Paste" Or UndoList = "Auto Fill" Then
MsgBox "Please don't paste values on this sheet." & vbCr & _
"The action will be reversed.", vbInformation, _
"Paste is not permitted"
With Application
.Undo
.CutCopyMode = False
End With
Target.Select
End If
ErrExit:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
How about using Application.Onkey.
Sub DisableCutCopyPaste()
Application.OnKey "^{c}", "" 'Copy
Application.OnKey "^{v}", "" 'Paste
Application.OnKey "^{x}", "" 'Cut
End Sub
I am (trying) creating a VBA code that filters Sheet1 based on the inputted password. I have an excel file with 2 Sheets and sheet2 has the passwords in Column B and the "filter" in column A. I will distribute the excel file and give the corresponding password to the parties and when they input their password all the info from other parties will be deleted.
The code:
Sub Open_with_password()
pas = Application.InputBox("Input password")
If pas = False Or pas = "" Then Exit Sub
Application.ScreenUpdating = False
a = 0
For i = 1 To Sheet2.Range("A1").End(xlDown).Row
If Worksheets("Sheet2").Cells(i, 2) = pas Then
c = Worksheets("Sheet2").Cells(i, 1) 'the agency corresponding with the password
a = a + 1
End If
Next
'Check for password
If a = 0 Then
MsgBox "Wrong password. Report can not be accessed"
ActiveWorkbook.Close False
Sheet2.Visible = xlSheetVeryHidden
Sheet1.Visible = xlSheetVeryHidden
Exit Sub
'If correct password
Else:
Sheet1.Visible = xlSheetVisible
Worksheets("Sheet1").Select
Worksheets("Sheet1").Unprotect Password = "XYZ"
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
'Filter according to input password
If c <> "Admin" Then ActiveSheet.Range("$A$2:$AQ$500000").AutoFilter Field:=17, Criteria1:=c
Set rCell = ActiveSheet.AutoFilter.Range.Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells(1, 1)
Rows(rCell.Row).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Sheet1").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A2").Select
'If Admin
If c = "Admin" Then
Sheet2.Visible = xlSheetVisible
Sheet1.Visible = xlSheetVisible
End If
End If
Application.ScreenUpdating = True
End Sub
The issues I've encountered so far are:
1. When I open the file, the input box doesn't automatically show, ideally it would show while the user sees nothing.
2. When it filters according to the password (The filter works) when it reaches the part where it's suppose to delete everything else, it doesn't. I am using a copy and paste method and an error pops (Error 1004)
Much appreciated for your help
Suggestions:
When the workbook opens call your macro.
Private Sub Workbook_Open()
Open_with_password
End Sub
I would keep your data intact on a hidden worksheet.
Sheet1.Visible = xlSheetVeryHidden
Copy the filtered cells to a different worksheet
Set rCell = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
rcell.Copy Sheet2.Range("A1")
When the workbook closes clear Sheet2.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheet2.Cells.ClearContents
End Sub
If you do it this way you users won't be able to access the hidden data when they open the workbook without enabling macros.
1.Code should be on the Workbook_Open() event, you may do a call for another sub -my suggestion-. In "ThisWorkbook" object:
Private Sub Workbook_Open()
Call Open_with_password
End Sub
2. If you are using copy-paste, you can't do a select in the middle, doing so will lost the clipboard (normal behavior in excel VBA), hence you will having nothing to paste for, thus the error.
Rows(rCell.Row).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Sheet1").Select
Range("A2").Select 'lost clipboard
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A2").Select
Change for
Rows(rCell.Row).Select
Range(Selection, Selection.End(xlDown)).Copy
Sheets("Sheet1").Range("A2").PasteSpecial Paste:=xlPasteValues
Excel.Application.CutCopyMode = False 'clears clipboard
EDIT:
It should work no matter if there are filters or not.
OT: Next step for you would be searching how to avoid select (this is so much time consuming).
I am answering my own question as I used the following solution and it seems to be working:
Private Sub Workbook_Open()
Call Open_with_password
End Sub
&
Sub Open_with_password()
Sheet2.Visible = xlSheetHidden
Sheet1.Visible = xlSheetHidden
Sheet3.Cells.ClearContents
Sheet1.Range("A1", "AQ1").Copy
Sheet3.Range("A1").PasteSpecial Paste:=xlPasteValues
Application.ScreenUpdating = False
pas = Application.InputBox("Input password")
If pas = False Or pas = "" Then Exit Sub
a = 0
For i = 1 To Sheet2.Range("A1").End(xlDown).Row
If Worksheets("Sheet2").Cells(i, 2) = pas Then
c = Worksheets("Sheet2").Cells(i, 1) 'the agency corresponding with the password
a = a + 1
End If
Next
'Check for password
If a = 0 Then
MsgBox "Wrong password. Report can not be accessed"
ActiveWorkbook.Close False
Sheet2.Visible = xlSheetVeryHidden
Sheet1.Visible = xlSheetVeryHidden
Exit Sub
'If correct password
Else:
Sheet1.Visible = xlSheetVisible
Worksheets("Sheet1").Select
Worksheets("Sheet1").Unprotect Password = "amazon"
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
'Filter according to input password
If c <> "Admin" Then ActiveSheet.Range("$A$2:$AQ$500000").AutoFilter Field:=17, Criteria1:=c
Set rCell = ActiveSheet.AutoFilter.Range.Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells(1, 1)
Rows(rCell.Row).Select
Range(Selection, Selection.End(xlDown)).Copy
Sheets("Sheet3").Range("A2").PasteSpecial Paste:=xlPasteValues
Excel.Application.CutCopyMode = False 'clears clipboard
Sheet1.Visible = xlSheetVeryHidden
'If Admin
If c = "Admin" Then
Sheet2.Visible = xlSheetVisible
Sheet1.Visible = xlSheetVisible
End If
End If
Application.ScreenUpdating = True
End Sub
Copying an existing password protected sheet to a new workbook as an unprotected sheet gives the following error when user tries to type in data in the new worksheet.
Error: "the cell or chart you're trying to change is on a protected sheet"
Click OK on the error message.
Please note that this error happens only once. click OK on the pop up error message and type again, then excel allows you to type data in the cells and save the sheet.
We have an excel (format .xls) file currently being used to create another excel spreadsheet when a button on a form in the same spreadsheet is clicked. It basically copies one password protected blank sheet (a template) to a new workbook as an unprotected sheet. The code below used to work with excel 2007(using .xls format). We recently upgraded from excel 2007 to excel 2013 and the problem appeared.
Private Sub cmd_Click()
Dim jBook As Workbook
Dim jsheet As Worksheet
CurrentWorkBook = ActiveWorkbook.Name
Workbooks(CurrentWorkBook).Unprotect jWorksheetPassword
'catch all for errors
On Error GoTo ErrEnd
Dim orginalScreenUpdating As Boolean
orginalScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False
If Range("Language").Value = "2" Then
'French
Set jsheet = TemplateFR
Else
'english
Set jsheet = TemplateEN
End If
jsheet.Visible = xlSheetHidden
'jSheet.Visible = xlSheetVisible
'Delete this line
jsheet.Unprotect jWorksheetPassword
Set jBook = Workbooks.Add(xlWBATWorksheet)
jsheet.Copy After:=jBook.Sheets(1)
jBook.Sheets(2).Visible = xlSheetVisible
Application.DisplayAlerts = False
jBook.Sheets(1).Delete
Application.DisplayAlerts = True
jsheet.Visible = xlSheetVeryHidden
'Delete this line
jBook.Sheets(1).Unprotect jWorksheetPassword
'Delete this line
'jsheet.Protect Password:=jWorksheetPassword
NoErrEnd:
Workbooks(CurrentWorkBook).Protect Password:=jWorksheetPassword, Structure:=True, Windows:=False
Application.ScreenUpdating = orginalScreenUpdating
Unload Me
Exit Sub
ErrEnd:
Workbooks(CurrentWorkBook).Protect Password:=jWorksheetPassword, Structure:=True, Windows:=False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox DataTable.Range("MSG4").Value, vbCritical, DataTable.Range("MSG4TITLE").Value
Unload Me
End Sub
The following lines of code activate the original workbook and this somehow clears the protection of the copied sheet with excel 2013 only. On Excel 2007 this causes the original workbook to be activated and confuses users, hence the check for 2013.
If Application.Version = "15.0" Then
Workbooks(CurrentWorkBook).Activate
'jBook.Activate
End If
This is a hack that happens to work. If some one finds a better solution please do post it here as well.
The full code listing is as follows:
Private Sub cmd_Click()
Dim jBook As Workbook
Dim jsheet As Worksheet
CurrentWorkBook = ActiveWorkbook.Name
Workbooks(CurrentWorkBook).Unprotect jWorksheetPassword
'catch all for errors
On Error GoTo ErrEnd
Dim orginalScreenUpdating As Boolean
orginalScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False
If Range("Language").Value = "2" Then
'French
Set jsheet = TemplateFR
Else
'english
Set jsheet = TemplateEN
End If
jsheet.Visible = xlSheetHidden
Set jBook = Workbooks.Add(xlWBATWorksheet)
jsheet.Copy After:=jBook.Sheets(1)
jBook.Sheets(2).Visible = xlSheetVisible
Application.DisplayAlerts = False
jBook.Sheets(1).Delete
Application.DisplayAlerts = True
If Application.Version = "15.0" Then
Workbooks(CurrentWorkBook).Activate
'jBook.Activate
End If
jsheet.Visible = xlSheetVeryHidden
NoErrEnd:
Workbooks(CurrentWorkBook).Protect Password:=jWorksheetPassword, Structure:=True, Windows:=False
Application.ScreenUpdating = orginalScreenUpdating
Unload Me
Exit Sub
ErrEnd:
Workbooks(CurrentWorkBook).Protect Password:=jWorksheetPassword, Structure:=True, Windows:=False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox DataTable.Range("MSG4").Value, vbCritical, DataTable.Range("MSG4TITLE").Value
Unload Me
End Sub
I have an Excel spreadsheet that contains 7 worksheets.
I need the script below to be applied to only some of the worksheets (Sheet6 & Sheet7) whenever the document is saved.
I've spent several hours trying different modifications, must of which simply did not work. The VBA debugger does not throw any errors, and when I test the script it never appears to run.
How can the script below be modified to run against specific worksheets, whenever I save the document from any of the worksheet tabs?
Thank you
VBA - Lock Cells & Protect Sheet On Save
The script below will lock cells that contain values, and then password protect the sheet before saving.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
On Error Resume Next
Dim Cell As Range
With ActiveSheet
.Unprotect Password:=""
.Cells.Locked = False
For Each Cell In Application.ActiveSheet.UsedRange
If Cell.Value = "" Then
Cell.Locked = False
Else
Cell.Locked = True
End If
Next Cell
.Protect Password:=""
'Protect with blank password, you can change it
End With
Exit Sub
End Sub
Script Source
Change the ActiveSheet and use a For Each loop like so:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
On Error Resume Next
Dim Cell As Range
For Each sh In Array("Sheet1", "AnotherSheet", "OtherSheet")
With Sheets(sh)
.Unprotect Password:=""
.Cells.Locked = False
For Each Cell In Application.ActiveSheet.UsedRange
If Cell.Value = "" Then
Cell.Locked = False
Else
Cell.Locked = True
End If
Next
.Protect Password:=""
End With
Next
End Sub
This should help you (you'll have messages to let you know when you are in the event and when it's started and over) :
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Cell As Range
MsgBox "Event Workbook_BeforeSave Launched", vbInformation + vbOKOnly, "Started"
On Error GoTo ErrHandler
ReTry:
With Sheet6
.Unprotect Password:=""
.Cells.Locked = False
For Each Cell In .UsedRange
If Cell.Value = "" Then
Cell.Locked = False
Else
Cell.Locked = True
End If
Next Cell
.Protect Password:=""
'Protect with blank password, you can change it
End With
With Sheet7
.Unprotect Password:=""
.Cells.Locked = False
For Each Cell In .UsedRange
If Cell.Value = "" Then
Cell.Locked = False
Else
Cell.Locked = True
End If
Next Cell
.Protect Password:=""
'Protect with blank password, you can change it
End With
MsgBox "Event Workbook_BeforeSave Over", vbInformation + vbOKOnly, "Finished"
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & " :" & vbCrLf & _
Err.Description
Resume ReTry
End Sub
The code can be significantly shorted (run time wise) by
Using SpecialCells rather than looping through each cell
avoiding setting blank cells as being locked twice (minor compared to first point).
updated
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
For Each sh In Array("Sheet1", "AnotherSheet", "OtherSheet")
With Sheets(sh)
.Unprotect
.Cells.Locked = True
On Error Resume Next
.Cells.SpecialCells(xlBlanks).Locked = False
On Error GoTo 0
.Protect
End With
Next
End Sub
I adapted some code found here http://www.vbaexpress.com/kb/getarticle.php?kb_id=379 for a neat solution that forces users to enable macros. This method is a really good way of getting around this problem. However, my additional requirements are: one particular user needs a more sensitive version of 2 sheets within the workbook. Depending on the answer to a MsgBox an InputBox requires password verification and then shows the sensitive versions of these sheets.
I believe the answer lies in how the workbook is saved and then how all sheets are unhidden upon re-opening the workbook (since two of the sheets are effectively password locked) but somehow now when I open this workbook the password used for unhiding the two sensitive versions of the sheets is required to open the workbook.
So I changed the password required for verification of the said sheets but the Original password is still required to open the workbook:
Any assistance in diagnosing this problem would be a help: Creating a spread-sheet and testing the code on your system shouldn't be too difficult and a good place to start!
Option Explicit
Const WelcomePage = "Macros"
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False
'Evaluate if workbook is saved and emulate default propmts
With ThisWorkbook
If Not .Saved Then
Select Case MsgBox("Do you want to save the changes you made to '" & .Name & "'?", _
vbYesNoCancel + vbExclamation)
Case Is = vbYes
'Call customized save routine
Call CustomSave
Case Is = vbNo
'Do not save
Case Is = vbCancel
'Set up procedure to cancel close
Cancel = True
End Select
End If
'If Cancel was clicked, turn events back on and cancel close,
'otherwise close the workbook without saving further changes
If Not Cancel = True Then
.Saved = True
Application.EnableEvents = True
.Close savechanges:=False
Else
Application.EnableEvents = True
End If
End With
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False
'Call customized save routine and set workbook's saved property to true
'(To cancel regular saving)
Call CustomSave(SaveAsUI)
Cancel = True
'Turn events back on an set saved property to true
Application.EnableEvents = True
ThisWorkbook.Saved = True
End Sub
Private Sub Workbook_Open()
'Unhide all worksheets
Application.ScreenUpdating = False
Call ShowAllSheets
Application.ScreenUpdating = True
Dim msg1, msg2, Pwd As String
Pwd = "5555"
Do
msg1 = MsgBox("Are you the Master User?", vbYesNo)
Loop Until msg1 = vbNo Or msg1 = vbYes
If msg1 = vbNo Then
Worksheets("Sensitive Sheet 1").Visible = xlVeryHidden
Worksheets("Sensitive Sheet 2").Visible = xlVeryHidden
ThisWorkbook.Unprotect Password:=Pwd
ElseIf msg1 = vbYes Then
Do
msg2 = InputBox("Please Enter the Password", "Password Checker", vbOKOnly)
Loop Until msg2 = Pwd
Worksheets("Sensitive Sheet 1").Visible = True
Worksheets("Sensitive Sheet 2").Visible = True
Worksheets("Standard Sheet 1").Visible = xlVeryHidden
Worksheets("Standard Sheet 2").Visible = xlVeryHidden
End If
End Sub
Private Sub CustomSave(Optional SaveAs As Boolean)
Dim WS As Worksheet, aWs As Worksheet, newFname As String
'Turn off screen flashing
Application.ScreenUpdating = False
'Record active worksheet
Set aWs = ActiveSheet
'Hide all sheets
Call HideAllSheets
'Save workbook directly or prompt for saveas filename
If SaveAs = True Then
newFname = Application.GetSaveAsFilename( _
fileFilter:="Macro Enabled Excel Files (*.xlsm), *.xlsm")
If Not newFname = "False" Then ThisWorkbook.SaveAs newFname
Else
ThisWorkbook.Save
End If
'Restore file to where user was
Call ShowAllSheets
aWs.Activate
'Restore screen updates
Application.ScreenUpdating = True
End Sub
Private Sub HideAllSheets()
'Hide all worksheets except the macro welcome page
Dim WS As Worksheet
Worksheets(WelcomePage).Visible = xlSheetVisible
For Each WS In ThisWorkbook.Worksheets
If Not WS.Name = WelcomePage Then WS.Visible = xlSheetVeryHidden
Next WS
Worksheets(WelcomePage).Activate
End Sub
Private Sub ShowAllSheets()
'Show all worksheets except the macro welcome page
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
If WS.Name <> "Sensitive Sheet 1" And WS.Name <> "Sensitive Sheet 2" Then
If Not WS.Name = WelcomePage Then WS.Visible = xlSheetVisible
End If
Next WS
Worksheets(WelcomePage).Visible = xlSheetVeryHidden
End Sub
You don't need to protect the whole workbook to hide the sheets. xlVeryHidden will hide the sheet and prevent it from being shown on the Hide/Unhide list.
First, unprotect the workbook. You can do this by the Review tab, and then clicking on the Protect Workbook and clear any protection you have checked. I'd also click on the Protect Sheet button for each sheet and clear any protection set there.
Then, modify your Workbook_Open subroutine to do something like this:
Private Sub Workbook_Open()
Dim msg1, msg2, Pwd As String
Pwd = "5555"
Do
msg1 = MsgBox("Are you the Master User?", vbYesNo)
Loop Until msg1 = vbNo Or msg1 = vbYes
If msg1 = vbNo Then
Worksheets("Sensitive Sheet 1").Visible = xlVeryHidden
Worksheets("Sensitive Sheet 2").Visible = xlVeryHidden
ElseIf msg1 = vbYes Then
Do
msg2 = InputBox("Please Enter the Password", "Password Checker", vbOKOnly)
Loop Until msg2 = Pwd
Worksheets("Sensitive Sheet 1").Visible = True
Worksheets("Sensitive Sheet 2").Visible = True
Worksheets("Standard Sheet 1").Visible = xlVeryHidden
Worksheets("Standard Sheet 2").Visible = xlVeryHidden
End If
End Sub
Basically, the workbook is unprotected when opened. If the user is not the special user, then it hides the two special sheets. If they are the special user, then it hides the two special sheets and they can't be unhidden.
Additional Suggestion
You'll run into a problem with the sheet visibility when the user saves - if it's the special user, once the save is complete they won't be able to see the sensitive sheets, because your ShowAllSheets subroutine will hide them.
To get around this, set a global variable to record whether the user is the special user or not, and use that to determine what sheets to show in ShowAllSheets, like this:
Option Explicit
Public IsMasterUser As String
Private Sub Workbook_Open()
Dim msg1, msg2, Pwd As String
Pwd = "5555"
IsMasterUser = "N"
Application.ScreenUpdating = False
Call ShowAllSheets
Application.ScreenUpdating = True
Do
msg1 = MsgBox("Are you the Master User?", vbYesNo)
Loop Until msg1 = vbNo Or msg1 = vbYes
If msg1 = vbNo Then
IsMasterUser = "N"
Worksheets("Sensitive Sheet 1").Visible = xlVeryHidden
Worksheets("Sensitive Sheet 2").Visible = xlVeryHidden
ElseIf msg1 = vbYes Then
Do
msg2 = InputBox("Please Enter the Password", "Password Checker", vbOKOnly)
Loop Until msg2 = Pwd
IsMasterUser = "Y"
Worksheets("Sensitive Sheet 1").Visible = True
Worksheets("Sensitive Sheet 2").Visible = True
Worksheets("Standard Sheet 1").Visible = xlVeryHidden
Worksheets("Standard Sheet 2").Visible = xlVeryHidden
End If
End Sub
Private Sub ShowAllSheets()
'Show all worksheets except the macro welcome page
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
If Not WS.Name = WelcomePage Then
If IsMasterUser = "N" _
And WS.Name <> "Sensitive Sheet 1" _
And WS.Name <> "Sensitive Sheet 2" Then
WS.Visible = xlSheetVisible
ElseIf IsMasterUser = "Y" _
And WS.Name <> "Standard Sheet 1" _
And WS.Name <> "Standard Sheet 2" Then
WS.Visible = True
End If
End If
Next WS
Worksheets(WelcomePage).Visible = xlSheetVeryHidden
End Sub
This code will set a flag (IsMasterUser) depending on the selection in Workbook_Open. Then when ShowAllSheets is called, it will determine whether to show the standard sheets or the special sheets based on IsMasterUser. Note that IsMasterUser is defaulted to "N" when its declared.
EDIT
A couple of minor problems with the code above. First, the global variable needs to be declared as Public IsMasterUser As String, and secondly it needs to be set to a value inside a function, so I set it to "N" at the start of the Workbook_Open subroutine.
I tested the posted code (the second set), along with the unaltered code from the original post in a Excel 2010 workbook with 6 sheets - Macros, User, Standard Sheet 1, Standard Sheet 2, Sensitive Sheet 1 and Sensitive Sheet 2, and it worked just fine.