[英]Hide and unhide rows in Excel VBA when specific value in the one cell
[英]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 個不能修改。 我想當用戶按下選項按鈕時顯示:
這是文件: 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.