将变量设置为Workbooks.Open

时间:2018-12-23 21:13:13

标签: excel vba

下面是我的但出现错误

  

编译错误:预计语句结束。

Set galreqws = Workbooks.Open FileName:=ThisWorkbook.Path & "\galreq.xlsx"

2 个答案:

答案 0 :(得分:1)

如果您要将var设置为Workbooks.Open方法的结果,请在参数中加上括号。

dim galreqws as workbook    'could also be dim galreqws as object
Set galreqws = Workbooks.Open(FileName:=ThisWorkbook.Path & "\galreq.xlsx")

'alternate
with  Workbooks.Open(FileName:=ThisWorkbook.Path & "\galreq.xlsx")
    debug.print .name
    debug.print .fullname
    debug.print .worksheets(1).name
    'work with galreq.xlsx here
end with

如果您未设置对象var,则将括号排除在外。

Workbooks.Open FileName:=ThisWorkbook.Path & "\galreq.xlsx"

答案 1 :(得分:0)

此后没有 空格

gnome-keyring-daemon --unlock

基本上替换:

Sub qwerty()
    Workbooks.Open Filename:=ThisWorkbook.Path & "\qwerty.xlsm"
    Set galreqws = ActiveWorkbook
End Sub

具有:

This workbook.Path

(我使用两行代码来避免混淆自己)