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

关于vba - 关闭 "At statup, open all files in:"文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42142712/

10-09 05:04