Windows(variables)。激活错误

Dim filepath As String filepath = ActiveWorkbook.Path + "\" Application.ScreenUpdating = False Dim file As String Dim fullfilepath As String file = Dir$(filepath & "*Example*" & ".*") fullfilepath = filepath & file Application.EnableEvents = False Workbooks.Open (fullfilepath) Windows(file).Activate Application.EnableEvents = True 

用上面的代码,很高兴地填充文件path,相当高兴地find文件“New Example.xlsm”,并且fullfilepath也正确结束。

当它到达Windows(文件)。激活时,我得到一个下标超出范围的错误(甚至没有进一步之前)

这是明显的东西吗? 它在另一个我的编码例子。

 Dim filepath As String filepath = ActiveWorkbook.Path + "\" Application.ScreenUpdating = False Dim file As String Dim fullfilepath As String file = Dir$(filepath & "*Example*" & ".*") fullfilepath = filepath & file Application.EnableEvents = False Dim NewWB As Workbook Set NewWB = Workbooks.Open (fullfilepath) Set ActiveWindow = NewWB Application.EnableEvents = True 

我相信这是一个更优雅,更容易合作。 如果您将NewWB声明为静态或公共variables,则可以将其用作多个模块或子目录的参考。 🙂