繁体   English   中英

如何根据不同工作表中的值隐藏和取消隐藏 Excel 中的特定表(在我的情况下为整行)? VBA

[英]How can I hide and unhide specific tables (in my case entire rows) in excel based on a value in a different sheet? VBA

我有一个文件,其中有 22 个表。 10 个表可以修改,12 个不能修改。 我想当用户按下选项按钮时显示:

  1. 所有表
  2. 可修改的表格(绿色)
  3. 不能修改的表(红色)

这是文件: https ://failiem.lv/u/gyybktdtf

如果选项按钮值在不同的工作表中,我会选择我喜欢的选项。 VBA 代码是什么,我应该把它放在什么地方。

在此处输入图像描述

在此处输入图像描述

显示/隐藏整行

  • 将代码复制到标准模块中,例如Module1 右键单击每个选项按钮并为其分配正确的过程。
Option Explicit

' Module Level Constants

Private Const YesRows As String _
        = "7:23,24:49,41:57,58:74,143:159," _
        & "177:193,211:227,245:261,279:295,313:329"
Private Const NoRows As String _
        = "75:91,92:108,109:125,126:142,160:176," _
        & "194:210,228:244,262:278,296:312,330:346," _
        & "347:364,365:381"
Private Const wbName As String = "Example.xlsx"
Private Const wsName As String = "Sheet2"

' Module Level Procedures (cannot be seen in the 'Assign Macro' dialog)

Private Function RefWorksheet() As Worksheet
    ' If you put the code in another macro-enabled workbook, 
    ' the workbook needs to be open, before you can use:
    Dim wb As Workbook: Set wb = Workbooks(wbName)
    ' If the code is in the same workbook, you will have to save it
    ' as a macro-enabled workbook, before you can instead use:
    'Dim wb As Workbook: Set wb = ThisWorkbook ' (recommended)
    Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
    Set RefWorksheet = ws
End Function

Private Sub ShowRows(ByVal ws As Worksheet, ByVal RowsAddress As String)
    ws.Range(RowsAddress).EntireRow.Hidden = False
End Sub

Private Sub HideRows(ByVal ws As Worksheet, ByVal RowsAddress As String)
    ws.Range(RowsAddress).EntireRow.Hidden = True
End Sub

' Public Procedures (can be seen in the 'Assign Macro' dialog)
' (Click each option button and assign it the correct procedure.)

Sub ShowAll()
    Dim ws As Worksheet: Set ws = RefWorksheet
    ShowRows ws, YesRows
    ShowRows ws, NoRows
End Sub

Sub ShowYes()
    Dim ws As Worksheet: Set ws = RefWorksheet
    ShowRows ws, YesRows
    HideRows ws, NoRows
End Sub

Sub ShowNo()
    Dim ws As Worksheet: Set ws = RefWorksheet
    HideRows ws, YesRows
    ShowRows ws, NoRows
End Sub

暂无
暂无

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

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