关闭 "At statup, open all files in:" 文件

Closing a "At statup, open all files in:" file

我目前在我的 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

您试图通过路径引用工作簿,但 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

Use Workbooks(index), where index is the workbook name or index number