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

问题描述 投票:0回答:2

我有一个Excel电子表格,其中的标签带有要填写的问题。

我计算了列数以确保我拥有正确的列,并使用调试器逐步遍历代码以查看其读出的值。这些值似乎都是按顺序排列的。

((大部分)内容是机密的,太多了,所以我将尝试通过这种方式向您显示:

这显示值。我正在使用“基础”列。相应列的字母为Y。This shows the values. I am working with the "basis" column. The letter of the corresponding column is Y

我想根据单元格的值隐藏或显示问题。 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
excel vba
2个回答
1
投票

与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

0
投票

这应该起作用:

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

此过程取决于01 only根据您的屏幕截图为偶数行。

注意:如果要隐藏all空行,请使用以下循环:

        For rowNum = beginRow To endRow
            With ws.Cells(rowNum, checkCol)
                .EntireRow.Hidden = .Value = 0
            End With
        Next rowNum
热门问题
推荐问题
最新问题