Excel : Bảo vệ tất cả những file trong một thư mục cùng một lúc

Trong Excel bạn có thể bảo vệ một file để ngăn chặn người khác biên soạn nhưng bạn lại muốn bảo vệ tất cả những file Excel cùng một lúc trong một thư mục thì làm thư thế nào .

 

Để làm được việc đó bạn cần tạo VBA , cách làm như sau :

Mở một file Excel mới , bấm tổ hợp phím Alt + F11 để mở Microsoft Visual Basic , bấm Insert > Module và bạn dán mã phía dưới

 

Sub ProtectAll()

    Dim xWorkBooks As Workbook

    Dim xExitFile As String

    Dim xPassWord As Variant

    Dim xStrPath As String

    Dim xFileDialog As FileDialog

    Dim xFile As String

    On Error Resume Next

    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)

    xFileDialog.AllowMultiSelect = False

    xFileDialog.Title = \"Select a folder [tuvantinhoc1088]\"

    If xFileDialog.Show = -1 Then

        xStrPath = xFileDialog.SelectedItems(1)

    End If

    If xStrPath = \"\" Then

        Exit Sub

    Else

        xStrPath = xStrPath + \"\\\"

    End If

    xPassWord = Application.InputBox(\"Enter password\", \"tuvantinhoc1088\", , , , , , 2)

    If (xPassWord = False) Or (xPassWord = \"\") Then

        MsgBox \"Password cannot be blank!\", vbInformation, \"tuvantinhoc1088\"

        Exit Sub

    End If

<span style=\"background-color: #ffff00;\">    xFile = \"*.xlsx\"</span>

    xExitFile = Dir(xStrPath & xFile)

    On Error Resume Next

    Application.ScreenUpdating = False

    Do While xExitFile <> \"\"

        Set xWorkBooks = Workbooks.Open(xStrPath & xExitFile)

        Application.DisplayAlerts = False

        xWorkBooks.SaveAs Filename:=xWorkBooks.FullName, Password:=xPassWord

        Application.DisplayAlerts = True

        Workbooks(xExitFile).Close False

        Set xWorkBooks = Nothing

        xExitFile = Dir

    Loop

    Application.ScreenUpdating = True

    MsgBox \"Successfully protect!\", vbInformation, \"tuvantinhoc1088\"

End Sub

 

Quay trở lại Excel , chạy macro trên , cửa sổ Select a folder xuất hiện yêu cầu bạn chọn thư mục muốn báo vệ tất cả các file Excel .

 

\"\"

 

Bạn vào mật khẩu để bảo vệ tất cả các file

 

\"\"

 

Bấm OK để hoàn tất , và khi mở một file đã được bảo vệ như trên nó sẽ xuất hiện màn hình hỏi mật khẩu

 

\"\"

 

Nếu bạn muốn bảo vệ những file Excel cũ có định dạng .xls thì thay thế

xFile=”*.xlsx” thành xFile=”.xls”.