首页 > 解决方案 > 如何隐藏和取消隐藏标签

问题描述

我有一份显示上个月信息的月度报告。(最终,我会将更新的信息提取到报告中。)

我希望自动隐藏上个月的标签并打开当月的标签。

例如:对于 2018 年 9 月。可见选项卡显示“Aug18”。我希望自动隐藏“Aug18”选项卡并取消隐藏“Sep18”选项卡。

标签: excelvba

解决方案


1) 将当前日期转换为我们可以用于测试“大于”类型逻辑 YYYYMM 的格式:

Dim currentDate As String
currentDate=Format(Now(), "YYYYMM")

2)循环浏览您的工作表:

Dim currentDate As String
currentDate=Format(Now(), "YYYYMM")

Dim ws as Worksheet
For Each ws in ThisWorkbook.Worksheets

Next ws

3)如果它是一个名为一个月的工作表,则在该循环内部进行转换测试。在这里使用InStr

Dim currentDate As String
currentDate = Format(Now(), "YYYYMM")

Dim ws As Worksheet
Dim thisTabDate As String
For Each ws In ThisWorkbook.Worksheets

    'Test to see if this is in the right format
    If InStr(1, "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", Left(ws.Name, 3)) Then            
    End If
 Next ws

4)如果是,则将选项卡的名称格式化为相同的 YYYYMM 并设置其可见性

Dim currentDate As String
currentDate = Format(Now(), "YYYYMM")

Dim ws As Worksheet
Dim thisTabDate As String
For Each ws In ThisWorkbook.Worksheets

    'Test to see if this is in the right format
    If InStr(1, "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", Left(ws.Name, 3)) Then
        'This is messy, but we are just cutting the tab name up, turning it into an actual date, and then formatting that.
        thisTabDate = Format(CDate(Left(ws.Name, 3) & "-01-" & Right(ws.Name, 2)), "YYYYMM")

        'Set the visible True/False to the result of the test
        ws.Visible = (thisTabDate >= currentDate)
    End If
 Next ws

推荐阅读