代码之家  ›  专栏  ›  技术社区  ›  Mayco Duran

基于单元格值显示行-Excel VBA

  •  0
  • Mayco Duran  · 技术社区  · 8 年前

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

    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
    

    提前感谢!

    医学博士

    2 回复  |  直到 8 年前
        1
  •  3
  •   Scott Craner    8 年前

    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
    

    当然,您可以使用过滤器进行同样的操作。

        2
  •  0
  •   BruceWayne    8 年前

    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
    
    推荐文章