【问题标题】:Excel 2016 Open specific sheet based on current dateExcel 2016 根据当前日期打开特定工作表
【发布时间】:2016-12-28 14:42:35
【问题描述】:

具有 26 张工作表的 Excel 2016 工作簿均命名为 Jan 2、Jan 16、Jan 30、Feb 13 等。在每个工作表单元格 B1 上也有相同的信息,分别是 1 月 2 日、1 月 16 日、1 月 30 日、2 月 13 日。在每张纸上还有更多的日期信息,工作表 Jan 2, B3 具有该周的第一个日期(1 月 2 日),然后在 1 月 3 日、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

【问题讨论】:

  • 欢迎来到 S.O!你试过什么吗?如果是,请提供代码。看看tourhow to ask。友情提示:StackOverflow 不是“我们为您编码”的服务提供商。 Introduction to VBAMid-Advanced Tutorials 和我的 personal favorite
  • 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
  • 我尝试了上面的代码,但我认为它把它当作#的“1 2”,我需要它把它看作“jan 2”
  • 您使用了错误的函数:month() 将返回它的“#”,月份名称使用 monthname
  • Format(Month(mday),"mmm") 也可以使用

标签: vba excel date


【解决方案1】:
    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

【讨论】:

    猜你喜欢
    • 2013-04-19
    • 2019-06-19
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多