簡體   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