【问题标题】:Show row based on cell value - Excel VBA根据单元格值显示行 - Excel VBA
【发布时间】:2016-10-14 02:54:06
【问题描述】:

我正在使用以下代码根据单元格值隐藏行:

Sub HideN()

Dim RowCnt As Long, uRng As Range

BeginRow = 8
EndRow = 232
ChkCol = 6

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = 0 Then
         If uRng Is Nothing Then
          Set uRng = Cells(RowCnt, ChkCol)
         Else
          Set uRng = Union(uRng, Cells(RowCnt, ChkCol))
         End If

        End If
    Next RowCnt

 If Not uRng Is Nothing Then uRng.EntireRow.Hidden = True

End Sub

如果我还想取消隐藏单元格值为 1 的行,我需要修改什么?

提前致谢!

医学博士

【问题讨论】:

    标签: vba excel rows show


    【解决方案1】:

    这将隐藏所有 0 并取消隐藏所有其他。

    Sub HideN()
    
    Dim RowCnt As Long
    Dim BeginRow&, EndRow&, ChkCol&
    
    BeginRow = 8
    EndRow = 232
    ChkCol = 6
    
        For RowCnt = BeginRow To EndRow
            Rows(RowCnt).Hidden = Cells(RowCnt, ChkCol).Value = 0
        Next RowCnt
    
    
    
    End Sub
    

    当然你也可以用过滤器做同样的事情。

    【讨论】:

    • 为布尔表达式的结果设置布尔值的一个很好的例子 - +1
    【解决方案2】:

    您可以添加一个额外的If 语句,不是吗?:

    Sub HideN()
    
    Dim RowCnt As Long, uRng As Range
    
    BeginRow = 8
    EndRow = 232
    chkcol = 6
    
    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, chkcol).Value = 0 Then
            If uRng Is Nothing Then
                Set uRng = Cells(RowCnt, chkcol)
            Else
                Set uRng = Union(uRng, Cells(RowCnt, chkcol))
            End If
        End If
        If Cells(RowCnt, chkcol).Value = 1 Then ' This is the new line to add
            Rows(RowCnt).EntireRow.Hidden = False
        End If
    Next RowCnt
    
    If Not uRng Is Nothing Then uRng.EntireRow.Hidden = True
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 2023-01-17
      • 2016-01-11
      • 1970-01-01
      • 2022-11-22
      相关资源
      最近更新 更多