受密码保护的秒

时间:2020-02-05 11:24:59

标签: excel vba password-protection

我有一个宏,可以保护相互链接的几个电子表格。我有两个问题:

问题1:无法打开大文件(fe 90,000 KB)

问题2:因为当宏试图保护下一个文件时文件之间相互链接,所以要求为每个受保护的链接文件输入密码。

有没有一种方法可以避免为活动工作簿中链接的每个文件输入密码?

这是我的代码:

Private Sub CommandButton1_Click()

Dim Y_N As String
Dim Nrow As Long

Y_N = Application.InputBox("Please state if you want to PROTECT or UNPROTECT the files")

Select Case Y_N
Case "PROTECT"
'Generate random password

Dim CharacterBank As Variant
Dim x As Long
Dim str As String
Dim basicpass(10) As Variant
Dim encrpass(10) As Variant
Dim lrow As Long
Dim newrow As Long

CharacterBank = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", _
  "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", _
  "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "@", _
  "#", "$", "%", "^", "&", "*", "A", "B", "C", "D", "E", "F", "G", "H", _
  "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", _
  "W", "X", "Y", "Z")

For x = 1 To 10

    Randomize

    'str = str & CharacterBank(Int((UBound(CharacterBank) - LBound(CharacterBank) + 1) * Rnd + LBound(CharacterBank)))
    pstInBank = Int((UBound(CharacterBank) - LBound(CharacterBank) + 1) * Rnd + LBound(CharacterBank))
    basicpass(x) = CharacterBank(pstInBank)
    'encr(x, 1) = CharacterBank(Int((UBound(CharacterBank) - LBound(CharacterBank) + 1) * Rnd + LBound(CharacterBank)))

    If CharacterBank(pstInBank) = "X" Or CharacterBank(pstInBank) = "Y" Or CharacterBank(pstInBank) = "Z" Then

        encrpass(x) = CharacterBank(pstInBank)
    Else
        encrpass(x) = CharacterBank(pstInBank + 3)
    End If

Next x

RandomString = Join(basicpass, "")
ThisWorkbook.Worksheets("Files").Range("I1") = RandomString

Workbooks.Open Filename:="xxxxx\Password Records.xlsx"

lrow = Workbooks("Password Records.xlsx").Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
newrow = lrow + 1

Workbooks("Password Records.xlsx").Worksheets("Sheet1").Range("A" & newrow) = ThisWorkbook.Worksheets("Files").Range("B1") & " " & ThisWorkbook.Worksheets("Files").Range("B2")
Workbooks("Password Records.xlsx").Worksheets("Sheet1").Range("B" & newrow) = Join(encrpass, "")
Workbooks("Password Records.xlsx").Worksheets("Sheet1").Range("C" & newrow) = Join(basicpass, "")

Workbooks("Password Records.xlsx").Save
Workbooks("Password Records.xlsx").Close

'Protect the files

Dim path As String
Dim masterfile As Workbook

Dim at As Integer
Dim th As Integer
Dim pctCompl As Single

Application.DisplayAlerts = False

Set masterfile = ThisWorkbook

For I = 5 To 20

    masterfile.Activate
    path = Worksheets("Files").Range("B" & I)
    Workbooks.Open Filename:=path
    ActiveWorkbook.SaveAs Filename:=path, password:=RandomString, WriteRespassword:=RandomString
    ActiveWorkbook.Save
    ActiveWorkbook.Close

Next I

0 个答案:

没有答案