如何打开每周更新的文件?

时间:2018-03-29 07:39:00

标签: excel vba excel-vba

我是VBA宏的基础。我想在每次单击时创建按钮,它会打开具有特定周数的文件。

当年的当周= 2018年3月29日=第13周

4月2日,我希望这个代码在第14周自动生成文件。我如何到达那里?

Sub refresh()

    Workbooks.Open Filename:= _
      "C:\Users\GMM\Desktop\weekly report\SUMMARY 2018\WK13 2018\WK13SUMMARY.xlsm" _
      , Notify:=False
    Windows("board (002) protect.xlsm").Activate
End Sub

我希望WK13 2018\WK13SUMMARY.xlsm成为WK("ww") 2018\WK("ww")SUMMARY.xlsm

并且在第14周,它将自动变为WK14 2018\WK14SUMMARY.xlsm

3 个答案:

答案 0 :(得分:0)

您可以使用WorksheetFunction.WeekNum Method获取日期的周数:

WorksheetFunction.WeekNum(Date)

返回今天日期的周数。

答案 1 :(得分:0)

尝试下面的代码来自动化文件名,以及文件夹名称(我看到它也会根据周的日期而变化)。

<强> 代码

Option Explicit

Sub refresh()

Dim wb As Workbook
Dim wbName As String
Dim LastFolderName As String

' get the file name of current week
wbName = "WK" & WorksheetFunction.WeekNum(Date) & "SUMMARY.xlsm"

' last folder's name according to current week and year
LastFolderName = "WK" & WorksheetFunction.WeekNum(Date) & " " & Year(Date)

Set wb = Workbooks.Open(Filename:="C:\Users\GMM\Desktop\weekly report\SUMMARY 2018\" & _
                        LastFolderName & "\" & wbName, Notify:=False)

wb.Activate ' <-- not sure why you would nee to Activate it

End Sub

答案 2 :(得分:0)

尝试使用以下

Sub refresh()
    Workbooks.Open Filename:= _
      "C:\Users\GMM\Desktop\weekly report\SUMMARY 2018\" & "WK" & WorksheetFunction.WeekNum(Now()) & " 2018\" & "WK" & WorksheetFunction.WeekNum(Now()) & "SUMMARY.xlsm" _
      , Notify:=False
    Windows("board (002) protect.xlsm").Activate
End Sub