Hide Rows if value is 0,

  • I want to hide row 10 if A10="0", and unhide if the value changes.


    I have come up with the following, but I cant seem to figure out how to watch multiple rows.


    This works the way I want it for one row.


    I want rows A10-A159 watched.


    Thanks again,
    Dan


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Range("$A$10") = "0" Then
    Range("$A$10").EntireRow.Hidden = True
    Else
    Range("$A$10").EntireRow.Hidden = False
    End If

    End Sub

  • Re: Hide Rows if value is 0,


    Two examples: The first code, placed in a general module, will hide all rows that have a zero in column A.




    The following placed in the sheet's private module will hide a row whenever a zero in entered into column A in the range A2:A12


    Code
    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Not Intersect(Target, Range("A2:A12")) Is Nothing Then
        If Target.Value = 0 Then Target.EntireRow.Hidden = True
    End If
    End Sub
  • Re: Hide Rows if value is 0,


    You are so quick!
    That gets me started in the right direction.


    Here is what I have so far. It works, but may not be the best method.
    I am open to any suggestions.


    I want to watch Rows 1:159. If column A = 0, then hide row
    If column A > 0 Then unhide


    Thanks Again,
    Dan




    Option Explicit


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)


    HideZeroRows

    End Sub



    Option Explicit

    Sub HideZeroRows()
    Dim CheckNum As Long, LastRow As Long

    Application.ScreenUpdating = False
    LastRow = 159

    For CheckNum = LastRow To 10 Step -1
    If Cells(CheckNum, 1).Value = 0 Then Cells(CheckNum, 1).EntireRow.Hidden = True
    If Cells(CheckNum, 1).Value > 0 Then Cells(CheckNum, 1).EntireRow.Hidden = False
    Next CheckNum
    Application.ScreenUpdating = True
    End Sub

  • Re: Hide Rows if value is 0,


    What you will work, but it probably is overkill (overwork). Each time you select a different cell on the worksheet the Worksheet_SelectionChange will rerun the macro even if no data have changed anywhere on the worksheet -- much less in column A. That's why I used the Worksheet_Change event, which will fire only if a value in column A is changed. The following will act on all the rows when, but only when, a value in A1:A159 is changed.


    Code
    Private Sub Worksheet_Change(ByVal Target As Excel.Range) 
    Dim CheckNum As Long
        If Not Intersect(Target, Range("A2:A159")) Is Nothing Then 
        For CheckNum = 159 To 1 Step -1 
            If Cells(CheckNum, 1).Value = 0 Then Cells(CheckNum, 1).EntireRow.Hidden = True 
        Next CheckNum 
        End If 
    End Sub


    The question is which Event Macro to use to serve your needs. Perhaps you only need to execute when the sheet is first activated???

  • Re: Hide Rows if value is 0,


    It just dawned on me that nothing in the macors we've discussed ever UNHIDES a row if the column A value changes from zero to something else. Perhaps the following would be good to consider.


  • Re: Hide Rows if value is 0,


    Thank you very much. I am now just learning about events. I have learned so much from this group, and really apprecaiate your support, and willingnes to teach others.


    Thanks again *thomach*!


    Dan

  • Re: Hide Rows if value is 0,


    Hello out there!


    I've been trying to re-work (changing C.EntireColumn.Hidden = C.Value = 0 to C.EntireRow.Hidden = C.Value = 0, etc.) the following code to HIDE COLUMNS where cells in the target range equal ZERO so that it will HIDE ROWS where cells in the target range (Named "UPC-SKU" or located at "A$22:A$141") equal ZERO.



    So far I've been unsuccessful. Particularly, problems came up in the debugger with this line:


    Code
    For Each C In Range(rngTest(i))


    Any suggestions?


    The goal is to have a macro tucked away inactive (standard module) until called up to run MANUALLY. I have used links to external files so that I can "Edit Links" to pull data from new files as necessary, but I want the freedom to see the file I publish to the field UNALTERED, before I hide un-used rows.


    I don't understand VBA Code event timing well enough to decide which of the above examples would be best.


    I appreciate any help you can offer!


    THANKS!
    James R. Smith

    Thanks!
    JRSmith729

  • Re: Hide Rows if value is 0


    Hi!


    For anyone who may be interested, I found a solution for my own situation in the following code posted elsewhere in the forum.


    The goal was a macro that could be called up manually to test a range as desired, rather than one that was contantly looking for a change to take place in the target cell range. I wanted to be able to hide rows AFTER editing, without having to worry about the macro hiding rows DURING the edit phase.



    ENJOY!
    JRSmith729

    Thanks!
    JRSmith729

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!