简体   繁体   English

vba excel在日期单元格值上隐藏工作表

[英]vba excel hide worksheet on date cell value

I am a vba newbie, but enjoy the thought of creating programs for other users to enjoy and make their workload easier. 我是vba新手,但是喜欢为其他用户创建程序以使他们享受并简化他们的工作量的想法。 I have a workbook that contains 14 worksheets. 我有一个包含14个工作表的工作簿。 I have been looking for vba code that hides 3 of these sheets according to a date value. 我一直在寻找根据日期值隐藏这些工作表中的3个的VBA代码。 The date value is situated on sheet 1 cell B7. 日期值位于工作表1单元格B7上。 The 3 sheets I wish to hide are sheets 2, sheets 3 and sheets 4. These 3 sheets are required to be made unhidden until 3 days before date in sheet 1 cell B7. 我要隐藏的3张纸是2张纸,3张纸和4张纸。这3张纸需要在日期1天之前的3天之前隐藏在1页的单元格B7中。 From the date value I want the workbook to hide the 3 sheets and can only be unhidden if a correct password is used to unhide these 3 sheets. 根据日期值,我希望工作簿隐藏3张纸,并且只有在使用正确的密码来取消隐藏这3张纸时才可以将其隐藏。 I have spent countless hours trawling many sites trying to find the correct code. 我花了无数小时来拖网许多站点,以查找正确的代码。 Please can you help? 请你帮忙

What you need to do is not possible with only Excel and with what you have given as requirements. 仅Excel和要求的条件是不可能做到的。 Even if the sheets are hidden, the users will still be able to Un-Hide them because EXCEL allows them to do so. 即使隐藏了工作表,用户仍然可以取消隐藏它们,因为EXCEL允许他们这样做。

If the users are okay with the possibility of the sheets to be un-hidden without the consent of the code, then go ahead and try this: 如果用户可以在未经代码许可的情况下取消隐藏工作表,可以继续尝试以下操作:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myDate As Date
    myDate = Range("b7").Value
    Dim curr_date As Date
    curr_date = Date


    If curr_date >= CDate((myDate) - 3) Then

        ActiveWorkbook.Sheets(2).Visible = xlSheetVeryHidden
        ActiveWorkbook.Sheets(3).Visible = xlSheetVeryHidden
        ActiveWorkbook.Sheets(4).Visible = xlSheetVeryHidden

    Else
        ActiveWorkbook.Sheets(2).Visible = xlSheetVisible
        ActiveWorkbook.Sheets(3).Visible = xlSheetVisible
        ActiveWorkbook.Sheets(4).Visible = xlSheetVisible

    End If
End Sub

Remember that you have to invoke VB Editor to type the above code. 请记住,您必须调用VB编辑器来键入上面的代码。 To do that, you will need to press Alt + F11 then you will be presented with a list of 14 sheets that you have. 为此,您需要按Alt + F11,然后会为您提供14张纸的清单。 Double click SHEET1 to be able to write the above code. 双击SHEET1可以编写上面的代码。 Just copy and paste it there. 只需复制并粘贴在那里。 Alternatively you could choose Worksheet from the combobox on the top left side of the bar, and Change from the top right side, then paste the code there. 或者,您可以从工具栏左上方的组合框中选择“ Worksheet ,然后从右上方选择“ Change ”,然后将代码粘贴到此处。

Here is a simplification image that will help you understand what I mean: 这是一个简化的图像,可以帮助您理解我的意思: 根据日期隐藏表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM