使用可选参数打开Excel VBA Workbooks.open

我确信这是一个非常基本的问题,但有一个非常基本的答案,但是目前为止,我无法让Excelmacros工作。

我正在尝试打开Excel工作簿并input可选的密码参数。 但是,每当我这样做,我得到错误编译错误预期:命名参数

这是我现在的代码

Sub password_opening_test() Workbooks.Open Filename:="E:\password protecting macrotest.xlsx",,,,Password:="test" Range("G7").Select ActiveCell.FormulaR1C1 = "hello" ActiveWorkbook.Save ActiveWindow.Close End Sub 

我也尝试了在逗号之间添加方形布置,但是我仍然收到错误(第一个逗号突出显示)。

非常感谢您的阅读

我认为有两个select可用:

  1. 正如斯科特在他的评论中的细节 – 摆脱你的逗号

只要在正确的位置使用参数

 Sub password_opening_test() Workbooks.Open "E:\password protecting macrotest.xlsx",,,,"test" Range("G7").Select ActiveCell.FormulaR1C1 = "hello" ActiveWorkbook.Save ActiveWindow.Close End Sub 

为了logging,我更喜欢Scott的方法,因为在将来对代码的再次访问中它更可读。 我喜欢使用行分隔符,所以它看起来像这样:

 Sub password_opening_test() Workbooks.Open _ Filename:="E:\password protecting macrotest.xlsx", _ Password:="test" Range("G7").Select ActiveCell.FormulaR1C1 = "hello" ActiveWorkbook.Save ActiveWindow.Close End Sub