检查工作簿是否存在,如果是,则检查它是否打开。 如果打开然后激活,如果closures然后打开它
我正在开发一个VBA程序,我需要做以下工作:
当button被点击(macros运行):
-
检查文件夹中是否存在MS EXCEL工作簿。 如果不是,则给出一个消息:“工作簿不存在”,VBA程序应该结束。
-
如果工作簿存在,则检查工作簿是closures还是打开。 如果它closures了,那么打开工作簿,VBA程序就应该移动得更远一些。
-
如果表单被打开,那么激活工作簿,VBA程序应该移动更多的步骤。
我已经写了这个到目前为止,但它不工作:
Sub test() Dim WbookCheck As Workbook On Error Resume Next Set WbookCheck = Workbooks("Weekly Report.xls") On Error GoTo 0 filepaths = "c:\clients\work\Weekly Report.xls" If Dir("filepaths") = False Then MsgBox "Please save the latest file under the name 'US Sector Flow Weekly Report' and run the macro again" Exit Sub ElseIf WbookCheck Is Nothing Then Workbooks.Open "c:\clients\work\Weekly Report.xls" Else WbookCheck.Activate End If Workbooks("Weekly Report.xls").Activate Sheets("This week").Select Sheets("This week").Copy Before:=Workbooks( _ "Consolidated.xls").Sheets(1) End Sub
Sub test() Dim WbookCheck As Workbook On Error Resume Next Set WbookCheck = Workbooks("Weekly Report.xls") On Error GoTo 0 If WbookCheck Is Nothing then 'not open.... filepaths = "c:\clients\work\Weekly Report.xls" If Dir(filepaths) = "" Then MsgBox "Please save the latest file under the name" & _ " 'US Sector Flow Weekly Report' and run the macro again" Exit Sub Else 'file exists - open it Set WbookCheck = Workbooks.Open(filepaths) End If End If with WbookCheck .Activate .Sheets("This week").Copy _ Before:=Workbooks("Consolidated.xls").Sheets(1) end with End Sub