使用VBA打开受密码保护的工作簿 [英] Open Password Protected Workbook with VBA
问题描述
我有wb1和wb2.wb1用于数据输入,而wb2用于记录该数据.wb2受密码保护(比如说"password").
I have wb1 and wb2. wb1 is used for data entry and wb2 is used to record that data. wb2 is password protected (let's say with "password").
下面的VBA在wb1中,单击即可运行.现在,在VBA运行完成之前,会弹出一个框,要求输入wb2的密码.是否可以使用VBA在该框中输入密码"?
The VBA below is in wb1 and is run on a button click. As it is now, a box pops up requesting the password for wb2 before the VBA is done running. Is there a way to use VBA to enter "password" in that box?
我刚接触VBA,因此也欢迎其他建议.谢谢!
I'm fairly new to VBA, so I'm open to other suggestions as well. Thanks!
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
推荐答案
打开 wb2
时,将您的"password"参数作为 Password
参数传递:
When opening wb2
pass your "password" argument as the Password
parameter:
Set wb2 = Workbooks.Open(Filename:="C:\Users...", Password:="password")
有关更多信息,请参见 Workbooks.Open方法(Excel)详细信息.
See Workbooks.Open Method (Excel) for more details.
这篇关于使用VBA打开受密码保护的工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!