Excel VBA Video Training/ EXCEL DASHBOARD REPORTS

Ozgrid, Experts in Microsoft Excel Spreadsheets

Password Protect Worksheet From Viewing

 

Excel VBA: Password Protect Worksheet From Viewing. Prevent Worksheet Viewing

Got any Excel Questions? Excel Help

Password Protect Worksheet From Viewing

With the aid of some Excel VBA code placed the private module of the Workbook Object (ThisWorkbook) we can password protect a Worksheet from viewing to all those that do not know the password.

IMPORTANT:
It should be noted that this method is far from secure and should not be used if the Worksheet contains highly sensitive information. It is also addition to the general Worksheet protection and uses the UserInterfaceOnly option of the Protect Method. You should also Protect/Lock Excel VBA Code

The code shown below makes use of the Worksheets Code Name .

It does not mask the password entry, but does stop after 3 failed attempts. If you wish to mask the password, use a Textbox on a UserForm that has its PasswordChar Property set to *. See Protect all Worksheets for details.

The Workbook_Open procedure is there to ensure the Workbook does not open with the un-viewable Worksheet being active.

To use this code: While in Excel proper, right click on the Excel icon, top left next to File and choose View Code it is in here you must paste the code below and change to suit your needs.

Dim sLast As Object



Private Sub Workbook_Open()

    'Ensure Sheet1 is not the active sheet upon opening.

    If Sheet1.Name = ActiveSheet.Name Then Sheet2.Select

End Sub



Private Sub Workbook_SheetActivate(ByVal Sh As Object)

Dim strPass As String

Dim lCount As Long



    If Sh.CodeName <> "Sheet1" Then

       'Set sLast variable to the last active sheet _

       This is then used to return the user to the _

       last sheet they were on if password is not known _

       or they Cancel.

       Set sLast = Sh

       

    Else

       'Hide Columns

       Sheet1.Columns.Hidden = True

           'Allow 3 attempts at password

            For lCount = 1 To 3

                strPass = InputBox(Prompt:="Password Please", Title:="PASSWORD REQUIRED")

                    If strPass = vbNullString Then 'Cancelled

                        sLast.Select

                        Exit Sub

                    ElseIf strPass <> "Secret" Then 'InCorrect password

                        MsgBox "Password incorrect", vbCritical, "Ozgrid.com"

                    Else 'Correct Password

                       Exit For

                    End If

            Next lCount

        

            If lCount = 4 Then 'They use up their 3 attempts

                sLast.Select

                Exit Sub

            Else 'Allow viewing

                Sheet1.Columns.Hidden = False

            End If

    End If

End Sub

Excel Dashboard Reports & Excel Dashboard Charts 50% Off Become an ExcelUser Affiliate & Earn Money

Special! Free Choice of Complete Excel Training Course OR Excel Add-ins Collection on all purchases totaling over $64.00. ALL purchases totaling over $150.00 gets you BOTH! Purchases MUST be made via this site. Send payment proof to [email protected] 31 days after purchase date.


Instant Download and Money Back Guarantee on Most Software

Excel VBA Video Training/ EXCEL DASHBOARD REPORTS

Excel Trader Package Technical Analysis in Excel With $139.00 of FREE software!

Microsoft � and Microsoft Excel � are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft

Some of our more popular products are below...
Convert Excel Spreadsheets To Webpages | Trading In Excel | Construction Estimators | Finance Templates & Add-ins Bundle | Code-VBA | Smart-VBA | Print-VBA | Excel Data Manipulation & Analysis | Convert MS Office Applications To...... | Analyzer Excel | Downloader Excel | MSSQL Migration Toolkit | Monte Carlo Add-in | Excel Costing Templates