根据日期值显示/隐藏工作表的宏

发布于 2024-11-25 14:26:02 字数 244 浏览 1 评论 0原文

我有一个由可执行文件创建的 Excel 工作簿,其中包含每月各天的数据在单独的工作表上。可执行文件的“表 1”还列出了该月的日期。我想编写一个宏,根据“表 1”中的日期显示/隐藏工作表。

例如,如果一月份的数据显示了第 1,2,3,4,5,11,12 天,则宏应仅显示第 1 天、第 2 天、第 3 天、第 4 天、第 5 天的相应工作表,并隐藏第 6 天到第 6 天Day10 并显示 Day11 和 Day12。任何指示表示赞赏。

谢谢。

I have an excel workbook created by an executable with data for days of the month on separate worksheets. 'Sheet 1' of the executable also has the days of the month listed. I would like to write a macro that will show/hide the worksheets based on the date in 'Sheet 1'.

For Instance, if the data for the month of Jan has days 1,2,3,4,5,11,12 displayed then the macro should show only the corresponding worksheets for Day1, Day2, Day3, Day4,Day5 and hide Day6 through Day10 and show Day11 and Day12. Any pointers are appreciated.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2024-12-02 14:26:02
public sub setSheetVisiblity()

  'Load the data from sheet 1 into a collection
  'I'm making the assumption that you just have days listed horizontally from 
  '1A to 1*

  Dim currentColumn as Integer
  Dim activeDayCollection as Collection

  currentColumn = 1
  Set activeDayCollection = new Collection

  While Cells(currentColumn, 1).Value <> ""

    activeDayCollection.add Cells(currentColumn, 1).Value 

    currentColumn = currentColumn + 1
  Wend

  'Make every sheet invisible/visible
  For each currentWorksheet as Worksheet in Worksheets

    If currentWorksheet.Name == "Day" + activeDayCollection.Item 1 Then
      currentWorksheet.Visible = true
      activeDayCollection.Remove 1
    Else
       currentWorksheet.Visible = false
    End If

  Next currentWorksheet
end sub

该代码的工作原理是假设第一个工作表中的日期按递增顺序排列,这些工作表被命名为 Day###,其中 ### 是天数,并且您可能需要添加另一行来手动取消隐藏您的工作表。第一张纸。我没有 vba,所以这段代码可能有一些语法错误,但它应该让你朝着正确的方向前进。

public sub setSheetVisiblity()

  'Load the data from sheet 1 into a collection
  'I'm making the assumption that you just have days listed horizontally from 
  '1A to 1*

  Dim currentColumn as Integer
  Dim activeDayCollection as Collection

  currentColumn = 1
  Set activeDayCollection = new Collection

  While Cells(currentColumn, 1).Value <> ""

    activeDayCollection.add Cells(currentColumn, 1).Value 

    currentColumn = currentColumn + 1
  Wend

  'Make every sheet invisible/visible
  For each currentWorksheet as Worksheet in Worksheets

    If currentWorksheet.Name == "Day" + activeDayCollection.Item 1 Then
      currentWorksheet.Visible = true
      activeDayCollection.Remove 1
    Else
       currentWorksheet.Visible = false
    End If

  Next currentWorksheet
end sub

The code works off of the assumption that the days in your first sheet are in increasing order, the sheets are named Day###, where ### is the day number, and you will probably have to add another line to manually unhide your first sheet. I don't have vba with me so this code might have some syntax errors, but it should get you going in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文