[英]How to hide and unhide rows based on a cell value?
我有一個 Excel 電子表格,其中包含要填寫問題的選項卡。
我計算了列數以確保我有正確的列,並使用調試器逐步遍歷代碼以查看它讀出的值。 這些值似乎都井然有序。
內容(大部分)是機密的,空的太多了,所以我將嘗試以這種方式向您展示:
我想根據單元格的值隱藏或顯示問題。 0 表示隱藏,1 表示顯示。
我發現代碼可以隨意隱藏和取消隱藏。
Sub CheckRoleQuestionsSimplified()
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Cockpit" Or ws.Name = "I-CBO" Or ws.Name = "config" Or ws.Name = "BegeleidingsFormulier" Or ws.Name = "Inhoudsopgave" Or ws.Name = "Saldibalans" Then
' Sla deze over, doe niks
Else
Worksheets(ws.Name).Activate
End If
Next ws
beginRow = 10
endRow = 46
checkCol = 25
Do While rowNum < endRow
If Cells(rowNum, checkCol).Value = 0 Then
Cells(rowNum, checkCol).EntireRow.Hidden = True
Else
If Cells(rowNum, checkCol).Value = 1 Then
Cells(rowNum, checkCol).EntireRow.Hidden = False
End If
End If
Next rowNum
End Sub
使用 With 語句。 並使用 isEmpty 函數。
Sub CheckRoleQuestionSimplified()
Dim Ws As Worksheet
Dim Target As Range
Dim rowNum As Integer, endRow As Integer, checkCol As Integer
For Each Ws In ThisWorkbook.Worksheets
Select Case Ws.Name
Case "Cockpit", "I-CBO", "config"
Case Else
With Ws
beginRow = 10
endRow = 46
checkCol = 25
For rowNum = beginRow To endRow
Set Target = .Cells(rowNum, checkCol)
Target.EntireRow.Hidden = False
If Not IsEmpty(Target) Then '<~~ check Empty
If Target.Value = 0 Then
Target.EntireRow.Hidden = True
Else
If Target.Value = 1 Then
Target.EntireRow.Hidden = False
End If
End If
End If
Next rowNum
End With
End Select
Next Ws
End Sub
這應該有效:
Dim ws As Worksheet
Dim beginRow As Long, endRow As Long, checkCol As Long
beginRow = 10
endRow = 46
checkCol = 25
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = "Cockpit" Or ws.Name = "I-CBO" Or ws.Name = "config" Or ws.Name = "BegeleidingsFormulier" Or ws.Name = "Inhoudsopgave" Or ws.Name = "Saldibalans" Then
' do nothing
Else
For rowNum = beginRow To endRow Step 2
With ws.Cells(rowNum, checkCol)
.Resize(2, 1).EntireRow.Hidden = .Value = 0
End With
Next rowNum
End If
Next ws
根據您的屏幕截圖,此過程依賴於0
或1
僅位於偶數行中。
注意:如果您希望隱藏所有空行,請改用以下循環:
For rowNum = beginRow To endRow
With ws.Cells(rowNum, checkCol)
.EntireRow.Hidden = .Value = 0
End With
Next rowNum
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.