关闭“在statup中,打开所有文件:”文件

时间:2017-02-09 17:06:01

标签: vba excel-vba xlsm excel

我目前在我的C驱动器上设置了名为“XL Startup”的文件夹。引用此选项可在Excel启动时打开此文件夹中的所有文件。

此文件夹中存在的文件名为“mymacros.xlsm”& “CopyMacro.xlsm”。这些是包含宏并在后台隐藏的文件,如此...

Private Sub Workbook_Open()
    Me.Activate
    ActiveWindow.Visible = False
End Sub

mymacros.xlsm将通过CopyMacro.xlsm中的宏进行更新。这将确保mymacros.xlsm保持最新。但是,当我调用mymacros.xlsm关闭时,我收到一条错误消息:Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.我怎样才能让它运行起来?

“CopyMacro.xlsm”中的代码:

Sub Copy_One_File()

If Dir("C:\XL Startup", vbDirectory) = "" Then
MsgBox "Please create a folder named 'XL Startup' at C:\"
Else

    'Close Current Opened Macro
    Workbooks("C:\XL Startup\mymacros.xlsm").Close SaveChanges:=False 'ERROR HERE

    'Copy File
    FileCopy "S:\newversion\mymacros.xlsm", "C:\XL Startup\mymacros.xlsm"

    'Re-open Macro
    Workbooks.Open "C:\XL Startup\mymacros.xlsm"

    MsgBox "msgbox file copied"
End If

End Sub

1 个答案:

答案 0 :(得分:4)

您尝试通过路径引用工作簿,但Workbooks()集合仅接受索引,因此您无法使用工作簿的路径 - >

Workbooks("C:\XL Startup\mymacros.xlsm").Close SaveChanges:=False

但您可以参考mymacros.xlsm索引 - >

Workbooks("mymacros.xlsm").Close SaveChanges:=False

Check the MSDS for Workbooks() collection reference

  

使用工作簿(索引),其中index是工作簿名称或索引号