Hi
I have a sheet that will force user to enable macro before revealing the sheets and enabling them to key in data.
But because I need to protect the workbook from user deleting sheets and also having some locked cells.
I got the run-time error 1004 unable to set visual property of worksheet class
Please help me to input the correct code to my file so that I can overcome this.
Thanks
my password to unprotect the file is 1234
This is the code for Sheet 1
This is the code for sheet 2
This is the code for "This Workbook"
I have a sheet that will force user to enable macro before revealing the sheets and enabling them to key in data.
But because I need to protect the workbook from user deleting sheets and also having some locked cells.
I got the run-time error 1004 unable to set visual property of worksheet class
Please help me to input the correct code to my file so that I can overcome this.
Thanks
my password to unprotect the file is 1234
This is the code for Sheet 1
Code:
Dim PreviousValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Errb As Integer
On Error GoTo ErrTrap:
If Target.Value <> PreviousValue Then
With Sheets("log").Cells(65000, 1).End(xlUp)
.Offset(1, 0).Value = Application.UserName
.Offset(1, 1).Value = "changed sheet/cell"
.Offset(1, 2).Value = ActiveSheet.Name
.Offset(1, 3).Value = Target.Address
.Offset(1, 4).Value = "from"
.Offset(1, 5).Value = PreviousValue
.Offset(1, 6).Value = "to"
.Offset(1, 7).Value = Target.Value
.Offset(1, 8).Value = "On"
.Offset(1, 9).Value = Now()
End With
End If
Exit Sub
ErrTrap:
ErrNum = Err
If ErrNum = 13 Then
'*** Multiple cells have been selected, treat them as one merged group*****
Resume Next
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
PreviousValue = Target.Value
End Sub
This is the code for sheet 2
Code:
Private Sub Worksheet_Activate()
pword = InputBox("Please Enter a Password", "Unhide Sheets")
If pword <> "1234" Then ActiveSheet.Visible = False
End Sub
This is the code for "This Workbook"
Code:
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
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:="Excel Files (*.xls), *.xls")
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 Not ws.Name = WelcomePage Then ws.Visible = xlSheetVisible
Next ws
Worksheets(WelcomePage).Visible = xlSheetVeryHidden
End Sub