用当前dateclosuresmacros

我正在使用一个macrosclosures几个文件,其中一个使用文件名中的当前date。 我需要macros单击工作簿,然后closures并保存它。 我想我几乎有它,但我只是不能让macros点击活动工作簿,哪个文件名将每天更改。 这是我的。

Dim ClosePath As String Dim ClosePathDate As String ClosePath = "File_": ClosePathDate = ClosePath & Format(Date, "YYYYMMDD") & ".xlsx" Windows("ClosePathDate").Activate Sheets("Sheet1").Select Range("A1").Select ActiveWorkbook.Close SaveChanges:=True 

我不知道如何使用“Windows(”ClosePathDate“)”我也试过Windows = ClosePathDate.Activate,没有运气。

请帮忙。

即使工作簿在另一个Excel实例中打开,这也可以工作。 顺便说一句,你不需要select它来closures它。

 Sub Sample() Dim ClosePath As String Dim ClosePathDate As String Dim xlObj As Object ClosePath = "File_": ClosePathDate = ClosePath & Format(Date, "YYYYMMDD") & ".xlsx" '~~> Replace "C:\" with the relevant path Set xlObj = GetObject("C:\" & ClosePathDate) xlObj.Application.Workbooks(ClosePathDate).Close SaveChanges:=False End Sub 

其他方式

 Sub Sample() Dim wb As Workbook Dim ClosePath As String Dim ClosePathDate As String ClosePath = "File_": ClosePathDate = ClosePath & Format(Date, "YYYYMMDD") & ".xlsx" Set wb = Workbooks(ClosePathDate) wb.Close SaveChanges:=False End Sub