- Joined
- Sep 18, 2006
- Messages
- 1
- Reaction score
- 0
Dears,
How can we implement using a macro, the protection for individual sheet visibility using a inputbox requesting a password before the sheet is displayed to the user.
I have tried this piece of code on a fresh excel sheet with no data in it and it works fine, but the same if i try on an existing document with formulas and lot of data, gives the following error.
Run-Time Error '1004': Unable to set the Hidden property of the Range Class.
i have commented the point of error, for your reference/locating error
Note :The Code is written in the workbook code place.
Dim sLast As Object
Private Sub Workbook_Open()
If ActiveSheet.CodeName <> "Sheet5" Or ActiveSheet.CodeName <> "Sheet6" Then Sheet5.Select
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim strPass As String
Dim lCount As Long
If Sh.CodeName = "Sheet5" Or Sh.CodeName = "Sheet6" Then
Set sLast = Sh
Else
'Hide Columns
If Sh.CodeName = "Sheet1" Then
Sheet1.Columns.Hidden = False 'Error Appears at this line
End If
strPass = InputBox(Prompt:="Password Please", Title:="PASSWORD REQUIRED")
If strPass = vbNullString Then 'Cancelled
sLast.Select
Exit Sub
ElseIf strPass <> "123" Then 'InCorrect password
MsgBox "Password incorrect", vbCritical, "hhhj"
sLast.Select
Exit Sub
Else 'Correct Password
If Sh.CodeName = "Sheet1" Then
Sheet1.Columns.Hidden = True
End If
End If
End If
End Sub
How can we implement using a macro, the protection for individual sheet visibility using a inputbox requesting a password before the sheet is displayed to the user.
I have tried this piece of code on a fresh excel sheet with no data in it and it works fine, but the same if i try on an existing document with formulas and lot of data, gives the following error.
Run-Time Error '1004': Unable to set the Hidden property of the Range Class.
i have commented the point of error, for your reference/locating error
Note :The Code is written in the workbook code place.
Dim sLast As Object
Private Sub Workbook_Open()
If ActiveSheet.CodeName <> "Sheet5" Or ActiveSheet.CodeName <> "Sheet6" Then Sheet5.Select
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim strPass As String
Dim lCount As Long
If Sh.CodeName = "Sheet5" Or Sh.CodeName = "Sheet6" Then
Set sLast = Sh
Else
'Hide Columns
If Sh.CodeName = "Sheet1" Then
Sheet1.Columns.Hidden = False 'Error Appears at this line
End If
strPass = InputBox(Prompt:="Password Please", Title:="PASSWORD REQUIRED")
If strPass = vbNullString Then 'Cancelled
sLast.Select
Exit Sub
ElseIf strPass <> "123" Then 'InCorrect password
MsgBox "Password incorrect", vbCritical, "hhhj"
sLast.Select
Exit Sub
Else 'Correct Password
If Sh.CodeName = "Sheet1" Then
Sheet1.Columns.Hidden = True
End If
End If
End If
End Sub