+ Reply to Thread
Results 1 to 9 of 9

Hiding Rows if a Value is Zero

  1. #1
    Registered User
    Join Date
    04-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    14

    Hiding Rows if a Value is Zero

    I have a spreadsheet with multiple tabs, on each tab there are some rows (not in continuous ranges) that need to be hidden if the value in column F of that row is zero.

    If the value in column F is non-zero, I want a dialog to pop-up indicating "Row # on worksheet Y is non-zero" and giving the user the option to 1) hide the row anyway, 2) highlight the row (make the cell in column A shaded red) or 3) cancel out of the macro.

    In total there are 9 worksheets and about 100 rows to analyse.

    I know I could create an if statement for each row that looks to F and either pops up the dialog or hides the row, but that would mean 100 if statement which strikes me as slow and inefficient to both code and execute.

    I'm wondering if anyone can suggest a more efficient approach to this problem.


    Thanks in advance!

  2. #2
    Registered User
    Join Date
    05-13-2011
    Location
    New Zealander in London
    MS-Off Ver
    Excel 2007
    Posts
    74

    Re: Hiding Rows if a Value is Zero

    I didn't know how to come up with a msgbox with 3 options for this, so instead created a simple form for you instead.

    The macro starts at F1 and keeps looking down the column until a blank cell is found. Each time it finds a non-zero value the form pops up with the 3 options: Hide, Highlight, Ignore.
    • Hide makes the entire row hidden
    • Highlight makes the cell in column A have a red background
    • Ignore just hides the form and moves on.

    Hopefully this is what you were looking for.
    Attached Files Attached Files
    Last edited by tarquinious; 06-14-2011 at 01:48 PM.

  3. #3
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Hiding Rows if a Value is Zero

    Hi johnnycanuck, the following code will hide rows with a 0 in them
    Please Login or Register  to view this content.
    As for your other requirements, it seems like a lot of dialog pop-ups, or can be a lot, for the user to decide on. At any rate, that will require a form with options on it, which may take a little while to design, at least for me it will. However, I can do it but I cannot promise you'll get it post-haste!
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Hiding Rows if a Value is Zero

    Never mind me!

  5. #5
    Registered User
    Join Date
    04-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Hiding Rows if a Value is Zero

    Hey guys, thanks for the very quick responses.

    They are both good solutions but the issue I'm struggling with is that the rows I need to analyse and hide are not in a continuous range.

    I.e. on one sheet, it is only rows 2, 3, 7, 9, 85, 89, 112-115, 189, 193-197, 200, 204, 205 that I need to look at and hide, if their F column value is 0. No matter what the value of row 1, 4, 5, 6, 8, 10-84, etc., even if it is 0, I don't want to hide it (or even look at it for that matter).

    On another sheet, it is a different set of rows to analyse/hide.

    Other than an if statement for each row, do you have any suggestions for an efficient way to loop through this disparate set of rows?

    As for the dialog popping up a lot, it is expected that the rows will have a zero value so it *shouldn't* pop up often. (If it does, I expect the user to choose to cancel the process and resolve the underlying issue).

    Thanks again

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Hiding Rows if a Value is Zero

    I may be wrong but if you want to only look at a determined batch of rows ie: 2,3 7,9,85,89, & etc then you'll have to create a whack of if statements. Otherewise the code will cycle through and look for any row with the value of 0 and then deal with it.

    If you want to be that specific, you'll most likely have to be that specific in your code.

  7. #7
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Hiding Rows if a Value is Zero

    Perhaps a mock workbook with mock data and your desired outcome would be applicable here. Can you upload one?

  8. #8
    Registered User
    Join Date
    04-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    14

    SOLVED - Re: Hiding Rows if a Value is Zero

    Thanks again for your help and suggestions.

    I ended up writing some code that utilizes GoSub to save me from having to write a custom IF statement for each row in my (long) list.

    Attached is what I wrote. I'll need to add some error handling and status updating, but otherwise this gets the job done.

    The form is just a simple 3-button dialog box that returns value 0, 1 or 2 based on the user's selection.

    I know using GoTo (and presumably GoSub) and .Select code is poor form, but I couldn't come up with better alternatives. Suggestions to make the code more efficient, robust or just better would be appreciated and welcomed.


    Thanks again


    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    09-06-2011
    Location
    manchester england
    MS-Off Ver
    Office 365
    Posts
    307

    Re: Hiding Rows if a Value is Zero

    Quote Originally Posted by Mordred View Post
    Hi johnnycanuck, the following code will hide rows with a 0 in them[code]Sub RowHider()
    Dim wb As Workbook!
    I have just tried this code and it nearly does what i require, however there are rows at the top and bottom of the work sheet that i don't want removing, is there a way for this to only run a group of rows i select?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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