Excel 2016工作簿,共26页,全部名为1月2日,1月16日,1月30日,2月13日,依此类推。在每个单元格B1上也分别具有相同的信息,1月2日,1月16日,1月30日,2月13日。在每张纸上还有更多的日期信息,工作表1月2日,B3具有该周的第一个日期(1月2日),然后进展为2周1月1日,1月4日1月5日结束于M3 1月14日仅跳过星期日。我希望工作表可以打开工作表,其中包含有关今天日期的信息。因此,当我在5月19日打开它时,例如它打开的工作表将是5月8日。
Dim ws As Worksheet
Dim mnth As String, dte As String, mday As String
mday = Now() - Weekday(Now(), 3)
mnth = Month(mday)
dte = Day(mday) tabstr = mnth & " " & dte
For Each ws In Worksheets
If ws.Name = tabstr Then ws.Select Exit For
End If
Next ws
答案 0 :(得分:0)
Sub CalendarUpdate()
' Created by Brad Tostenson 12/28/2016
' Credit "Johnny B"
' calendarUpdate Macro
' The following code performs 2 basic operations;
' OPERATION 1: Opens the Calendar Of Time Off workbook associated with this workbook, updates the information in it and closes it.
' OPERATION 2: Sets the opening page to the current period of time for the Calendar Of Time Off workbook.
' OPERATION 1
' _________________________________________________________________________________
' Turns off alerts so that the user does not receive any dialog boxes
Application.DisplayAlerts = False
' Saves the TimeOff workbook
ActiveWorkbook.Save
' Opens the Calendar workbook so that it can be updated with the new information
Workbooks.Open Filename:="S:\Calendar Of Time Off.xltx"
' OPERATION 2
' _________________________________________________________________________________
' variable setup
Dim ws As Worksheet
Dim mnth As String, dte As String, mday As Date
' OPERATION 2.1 (Previous week's Monday)
' Finds todays date, from that it finds the previous week's Monday e.g. (if today is Thursday December 29, mday would = December 19)
mday = Now() - Weekday(Now(), 3) - 7
' OPERATION 2.11 (a portion of this operation is duplicated in operation 2.2)
' Based on the outcome of "mday" it gets the month information and then makes the three letter version = "mnth" e.g. (if mday = December 19, mnth = Dec)
mnth = MonthName(Month(mday), True)
' Based on the outcome of "mday" it gets the day information and then makes that = "dte" e.g. (if mday = December 19, dte = 19)
dte = Day(mday)
' Uses the information in "mnth" & "dte" to create a value that will match the syntax used in the naming of the tabs e.g. (Dec 19)
tabstr = mnth & " " & dte
' The following code uses the "tabstr" (Dec 19) information and will look for a match in the tab names and then opens the workbook to that tab
For Each ws In Worksheets
If ws.Name = tabstr Then
ws.Select
Exit For
End If
Next
' OPERATION 2.2
' Finds todays date, from that it finds the this week's Monday e.g.(if today is Thursday December 29, mday would = December 26)
mday = Now() - Weekday(Now(), 3)
mnth = MonthName(Month(mday), True)
dte = Day(mday)
tabstr = mnth & " " & dte
For Each ws In Worksheets
If ws.Name = tabstr Then
ws.Select
Exit For
End If
Next
' _________________________________________________________________________________
' OPERATION 1 (CONTINUED)
' Saves the Calendar of Time Off Sheet as a template to avoid file open errors
ActiveWorkbook.SaveAs Filename:="S:\Calendar Of Time Off", FileFormat:=54
ActiveWorkbook.Close
End Sub