使用VBA打开受密码保护的工作簿

时间:2015-12-03 19:28:56

标签: excel vba excel-vba

我有wb1和wb2。 wb1用于数据输入,wb2用于记录该数据。 wb2受密码保护(请说"密码")。

下面的VBA在wb1中,并在点击按钮时运行。就像现在一样,在VBA运行完毕之前弹出一个请求wb2密码的框。有没有办法使用VBA输入密码"在那个盒子里?

我对VBA很新,所以我也对其他建议持开放态度。谢谢!

Sub OpenClose()

Application.ScreenUpdating = False

Dim wb1 As Workbook
Dim wb2 As Workbook

Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("C:\Users...")  'I have taken out the rest of the file path

wb1.Activate
    Sheets("Entry").Activate
        Range("A1:A5").Select
            Selection.Copy

wb2.Activate
    Sheets("Log").Activate
        Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=True

wb2.Save
wb2.Close

Set wb1 = Nothing
Set wb2 = Nothing

MsgBox "Logged and saved."

Application.ScreenUpdating = True

End Sub

3 个答案:

答案 0 :(得分:10)

打开wb2时,将您的“密码”参数作为Password参数传递:

Set wb2 = Workbooks.Open(Filename:="C:\Users...", Password:="password")

有关详细信息,请参阅Workbooks.Open Method (Excel)

答案 1 :(得分:0)

这适用于需要密码才能打开的文件。打开wb2时,将您的“密码”参数作为Password参数:

Set wb2 = Workbooks.Open(Filename:="C:\Users...", Password:="password")

如果文件设置为以只读方式打开,但需要密码才能对其进行修改,请使用以下命令:

Set wb2 = Workbooks.Open(Filename:="C:\Users...", writeresPassword:="password")

答案 2 :(得分:-1)

结帐此代码可能会解决您正在寻找的问题

ThisWorkbook.Password = "1"
ThisWorkbook.Save
ThisWorkbook.Close