+ Reply to Thread
Results 1 to 9 of 9

Need help with this code for hiding rows with "0"

  1. #1
    Registered User
    Join Date
    04-19-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    Need help with this code for hiding rows with "0"

    I initially had a simple filter that I have been using to hide rows with 0 in Column I. Column I has formulas that either return values, 0 or 1. When I filter out "0", rows with no information are hidden and it is ready to print. I have written the following code but it only works when I make a change on that sheet. All of the data on the sheet is referenced from another sheet. How do I get the code to run when I change entries on that sheet?

    Please Login or Register  to view this content.
    In addition, this code does correctly hide the rows I want but the report can and will grow. If it gets longer than 36 rows, I assume the code will not work. What is the right syntax to always the Range set as Column I from Cell 5 to the last row with data?



    Thanks everyone!

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,942

    Re: Need help with this code for hiding rows with "0"

    Use this in the code module of the sheet where you have your data that is being entered. Change

    Worksheets("Sheet that you want to filter")
    to
    have the actual sheet name...

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    04-19-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Need help with this code for hiding rows with "0"

    Thanks. Seems to be just what I was looking for!

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,942

    Re: Need help with this code for hiding rows with "0"

    Actually, this may be better - hidden rows at the bottom of your list may not reset otherwise, since they will be missed by the End-Up combo...

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim RngCell As Range
    With Worksheets("Sheet that you want to filter")
    .Cells.EntireRow.Hidden = False
    For Each RngCell In .Range(.Range("I5"), .Cells(.Rows.Count, "I").End(xlUp))
    RngCell.EntireRow.Hidden = RngCell = 0
    Next RngCell
    End With
    End Sub

  5. #5
    Registered User
    Join Date
    04-19-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Need help with this code for hiding rows with "0"

    Hi again,

    Thanks for the additional thought. I am not sure I follow your reasoning for the change.

    Maybe first can you explain .Cells(.Rows.Count, "I")?

    I understand that End(xlUP) is the same as the keyboard shortcut ctrl+up arrow. If i am in Column I, using that shortcut is always going to take me to I6, which is the first row moving up before the nearest empty cell (I5).

    I am attaching a workbook to put this into context.

    Thanks again!
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,942

    Re: Need help with this code for hiding rows with "0"

    Actually, the range that this will return:

    .Cells(.Rows.Count, "I").End(xlUp)

    is the lowest visible cell in column I of the worksheet "Networth", because it is moving up from cell I1048576, which is cell I35 - it is not moving up from I35 to find I6.

    But, your row 36 is hidden. If you had a formula in I36, one that could return a non-zero value, it would never be un-hidden because the code would skip it. Based on your workbook, my concern really should not be a problem, but I just wanted to point it out to you. And it never hurts to completely reset all the rows to visible before hiding them again.

  7. #7
    Registered User
    Join Date
    04-19-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Need help with this code for hiding rows with "0"

    got it. thanks!

    So is specifying the range as I5 necessary. Can't I just use "I"?

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,942

    Re: Need help with this code for hiding rows with "0"

    You should start with whatever cell at the top has a value, to combine with the cell found at the bottom to completely cover filled cells. If you do all of I, blank rows at the top of I may hide headers in other columns.

    And you can never just use "I" - at best, you can use "I:I", but you also need to make sure that you are not processing the million + cells of the entire column. Stick with what I posted.

  9. #9
    Registered User
    Join Date
    04-19-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Need help with this code for hiding rows with "0"

    Awesome. Thank you for the explanation! You rock.

    Wanna have a go on this thread? =)

    http://www.excelforum.com/excel-prog...ent-sheet.html

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 04-16-2014, 10:00 AM
  2. Replies: 1
    Last Post: 09-21-2013, 03:18 AM
  3. Hiding Rows With A Cell Containing "0"
    By ExcelBob in forum Excel General
    Replies: 10
    Last Post: 04-23-2008, 01:07 PM
  4. [SOLVED] transpose the code from "rows" to "columns"
    By markx in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-21-2005, 05:05 AM
  5. hiding/unhiding rows & columns with "+" and "-" buttons
    By shellshock in forum Excel General
    Replies: 2
    Last Post: 08-03-2005, 01:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1