Consulting

Results 1 to 10 of 10

Thread: Hide Rows Where Column A = Zero

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Hide Rows Where Column A = Zero

    But NOT where Column A is blank!

    Option Explicit
    Sub HideRowsWithZeros()
    Dim ws As Worksheet
    Dim c As Range
    Dim rngRange As Range
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
        ws.Select
        Set rngRange = Range(Cells(1, 1), Cells(65336, 1).End(xlUp))
        For Each c In rngRange
            If c.Value = 0 Then
                c.EntireRow.Hidden = True 
            End If
        Next c
    Next ws
    Application.ScreenUpdating = True
    End Sub
    And is this the best way to do it?
    Last edited by Aussiebear; 04-29-2023 at 07:00 PM. Reason: Adjusted the code tags
    ~Anne Troy

  2. #2
    VBAX Regular
    Joined
    May 2004
    Location
    Seattle, WA
    Posts
    24
    Location
    Here is another way

    Sub HideRowsWithZeros2()
        Range(Cells(1, 1), Cells(65336, 1).End(xlUp)).Select
        Selection.AutoFilter
        Selection.AutoFilter Field:=1, Criteria1:="0"
        Selection.EntireRow.Delete
    End Sub
    Last edited by Aussiebear; 04-29-2023 at 07:01 PM. Reason: Adjusted the code tags

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Sorry. But if the cell say 0.00, then this does not hold. If the VALUE in A1 is zero, but NOT when it is blank.
    ~Anne Troy

  4. #4
    VBAX Regular
    Joined
    May 2004
    Location
    Seattle, WA
    Posts
    24
    Location
    There is a problem with the code that the first row is deleted. That row is deleted also. Also I forgot to turn off AutoFilter. I can fix this later but I need to run out of the office. Maybe someone else can fix it?

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    No rush! Thanks!
    ~Anne Troy

  6. #6
    VBAX Regular NateO's Avatar
    Joined
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    90
    Location
    Hello Anne,

    Quote Originally Posted by Dreamboat
    Sorry. But if the cell say 0.00, then this does not hold. If the VALUE in A1 is zero, but NOT when it is blank.
    As text? Coerce column A's values first then.

    Try the following:

    Sub HideRowsWithZeros3()
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    On Error Resume Next
    For Each ws In ThisWorkbook.Worksheets
        With ws
            .AutoFilterMode = False
            With .Range("a1")
                  .EntireRow.Hidden = (CBool(Len(.Value)) And .Value = 0)
              End With
              Range(.Cells(1, 1), .Cells(65336, 1).End(xlUp)) _
                  .AutoFilter Field:=1, Criteria1:="<>0", _
                  visibleDropDown:=False
        End With
    Next
    Application.ScreenUpdating = True
    End Sub
        
    
     Sub HideRowsWithZeros3()
      Dim ws As Worksheet
      Application.ScreenUpdating = False
      On Error Resume Next
      For Each ws In ThisWorkbook.Worksheets
          With ws
              .AutoFilterMode = False
              With .Range("a1")
                  .EntireRow.Hidden = (CBool(Len(.Value)) And .Value = 0)
              End With
              Range(.Cells(1, 1), .Cells(65336, 1).End(xlUp)) _
                  .AutoFilter Field:=1, Criteria1:="<>0", _
                  visibleDropDown:=False
          End With
      Next
      Application.ScreenUpdating = True
      End Sub
    Edit: VBA tags are getting readable on Netscape. The one thing is that it tabs as it thinks I should tab, not as I actually tab.
    Last edited by Aussiebear; 04-29-2023 at 07:04 PM. Reason: Adjusted the code tags
    Regards,
    Nate Oliver

  7. #7
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    That appears to work, Nate.
    I'll be back.
    ~Anne Troy

  8. #8
    VBAX Regular
    Joined
    May 2004
    Location
    Seattle, WA
    Posts
    24
    Location
    Thanks Nate.

    I really need to make sure I have enough time to answer a post completely before I start working on it. Bad me.

  9. #9
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    Edit: VBA tags are getting readable on Netscape. The one thing is that it tabs as it thinks I should tab, not as I actually tab.
    This is by design Nate. Is to make code nicely formatted when it's not been nicely tabbed already. I guess the only difference is the _ extended lines. Might add something in for this.

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  10. #10
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Okay, Nate. Would you mind adding your code to the KB? Puhlllleeeeeeeeeeeeze? Whippy and cherry on top!!
    ~Anne Troy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •