محافظت برگه های پنهان در Excel با کلمه عبور- کد VBA

در این مطلب یک ماکرو ساده ارائه شده که به شما امکان می‌دهد برای مشاهده کاربرگ‌های (Sheet) مخفی به رمز عبور نیاز داشته باشید.

این یک راه حل خوب رابط کاربری و مدیریت داده را به کتاب کار شما اضافه می‌کند و من به شما نشان می‌دهم که چگونه آن را در هر کتاب کار اعمال کنید.

 

این کار به این صورت است که به کاربر یک پنجره ورودی رمز عبور نشان می‌دهد. و اگر رمز عبور صحیح را وارد کند، می‌تواند کاربرگ های پنهان را مشاهده کند، در غیر این صورت کاربرگ ها پنهان می‌مانند و قابل مشاهده نیستند.

کاربرگ های مخفی در این آموزش را نمی‌توان با کلیک راست بر روی برگه برگه و کلیک بر روی Unhide مشاهده کرد و با دستکاری خود پروژه نمی‌توان آنها را مشاهده کرد، زیرا آن نیز محافظت می‌شود.

کد برای نمایش/پنهان کردن کاربرگ ها با استفاده از رمز عبور

' Excel VBA Tutorial - https://www.techyaran.ir
'
' Hide Worksheets Using a Password
'
' Specific Sheets will not be visible until the user enters the correct password.
'
' If you don't password protect the project, the user can still unhide the
' worksheet.
'
' Password Protect the Project: Right-click the Project in the Project Explorer >
' VBAProject Properties > Protection tab > check Lock project for viewing and
' input a password and hit OK.
 
Option Explicit ' Require that variables are declared.
 
' The Password
' A global constant value allows you to store this password once for your entire
' project and use it wherever you need to check for the password. This ensures
' that you only have to change the password in one place in the future.
Public Const conSheetPassword As String = "123456"
 
Sub ViewSheet()
' Code from: https://www.TeachExcel.com
'
' Unlock/show the worksheet if the user inputs the correct password.
 
    ' Variables.
    Dim userInput As Variant
    
    ' Ask the user for the password to unlock the sheet.
    userInput = InputBox( _
        Prompt:="Input a password to unlock the worksheets.", _
        Title:="Password Input", _
        Default:="Password")
 
    ' Check if the correct password was input.
    ' - Trim() removes any space that might have been accidentally added to the
    '   start or end of the password.
    ' - LCase() ensures that a case-insensitive check is being performed because
    '   it converts the input and the password both to lower case.
    If LCase(Trim(userInput)) = LCase(conSheetPassword) Then
        ' Pass - all is good.
        
        ' Show the Sheet.
        Sheets("Helper").Visible = xlSheetVisible
        Sheets("Raw").Visible = xlSheetVisible
        
        ' Go to the sheet.
        Sheets("Helper").Select
    
    Else
        ' Fail - wrong password.
        
        ' Let the user know.
        MsgBox "Incorrect password. Access Denied."
    
    End If
 
End Sub
Sub HideSheet()
' Code from: https://www.TeachExcel.com
'
' Hide the worksheet so that the user cannot unhide it by hand.
'
' If you don't password protect the project, the user can still unhide the
' worksheet.
'
' Password Protect the Project: Right-click the Project in the Project Explorer >
' VBAProject Properties > Protection tab > check Lock project for viewing and
' input a password and hit OK.
    
    ' Hide the sheet so the user can't unhide it by hand (so long as the Project
    ' code here is also password protected).
    Sheets("Helper").Visible = xlSheetVeryHidden
    Sheets("Raw").Visible = xlSheetVeryHidden
 
End Sub
 

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *