简体   繁体   English

如何根据单元格值隐藏和取消隐藏行?

[英]How to hide and unhide rows based on a cell value?

I have an Excel spreadsheet that has tabs with questions to fill out.我有一个 Excel 电子表格,其中包含要填写问题的选项卡。

I counted the columns to make sure I had the right one and looped through the code using the debugger step by step to see the values it read out.我计算了列数以确保我有正确的列,并使用调试器逐步遍历代码以查看它读出的值。 The values all appeared to be in order.这些值似乎都井然有序。

The contents are (mostly) confidential and it is too much to empty so I will attempt to show you this way:内容(大部分)是机密的,空的太多了,所以我将尝试以这种方式向您展示:

This shows the values.这显示了值。 I am working with the "basis" column.我正在处理“基础”列。 The letter of the corresponding column is Y.对应列的字母是 Y。
这显示了值。我正在处理“基础”列。对应列的字母是Y

I want to hide or show questions based on the value of the cell.我想根据单元格的值隐藏或显示问题。 0 means hide and 1 means show. 0 表示隐藏,1 表示显示。

I have found that the code hides and unhides at will.我发现代码可以随意隐藏和取消隐藏。

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

Use With statement.使用 With 语句。 and use isEmpty function.并使用 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

This should work:这应该有效:

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

This process relies upon the 0 or 1 only being in even numbered rows, as per your screenshot.根据您的屏幕截图,此过程依赖于01位于偶数行中。

Note: if you want all empty rows hidden, use the following loop instead:注意:如果您希望隐藏所有空行,请改用以下循环:

        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.

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