+ Reply to Thread
Results 1 to 11 of 11

Excel 2007 : Hiding rows based on values in cells

Hybrid View

  1. #1
    Registered User
    Join Date
    10-05-2011
    Location
    Washington, D.C.
    MS-Off Ver
    Excel 2007
    Posts
    12

    Hiding rows based on values in cells

    Hello,

    I have been trying to find a solution to the following problem since yesterday. So I finally came up with this code:

    Sub Worksheet_Hide()
    Dim lastRow As Long, myRange As Range
    Application.Calculation = xlCalculationManual
    lastRow = Cells(Rows.Count, 2).End(xlUp).Row
    Rows.Hidden = False
    For Each myRange In Range("g11:r62")
    If myRange.Value = 0 Then myRange.EntireRow.Hidden = True
    Next myRange
    End Sub

    Here is my problem. The range of the table that I need to check is g11:r62. If any cell in that range is populated then I don't want the row collapsed, but if every cell in the row is zero, then I would like it to be collapsed. As you can see now from looking at the code, it collapses everything. Every cell is hidden if any cell is empty.

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Hiding rows basedon values in cells...etc

    Try this:
    Sub Worksheet_Hide()
        Dim lastRow As Long, myRange As Range
        Application.Calculation = xlCalculationManual
        lastRow = Cells(Rows.Count, 2).End(xlUp).Row
        Rows.Hidden = False
        For Each c In Range("G11:G" & lastRow)
            For Each d In Range("G" & c.Row & ":R" & c.Row)
                If d.Value = 0 Then
                    d.EntireRow.Hidden = True
                    Exit For
                End If
            Next
        Next
    End Sub
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Hiding rows basedon values in cells...etc

    By the way, welcome to the forum! Please take a moment to review the forum rules since your code should have been enclosed in code tags [ code ] [/ code ]

  4. #4
    Registered User
    Join Date
    10-05-2011
    Location
    Washington, D.C.
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Hiding rows basedon values in cells...etc

    Thanks, but that code just hides rows 5 and 6. I need it to hide rows 12-62, if any cell in the range of g12:r62 is blank.

    Example: if there is a 0 value in all cells g12:r12, then I need it to hide row 12. However, if even one cell has a non-0 value, for example m12, then I need the row to show. Hope that clears it up a little.

    Ah, sorry about that. I'll make sure I post my code appropriately in the future.

  5. #5
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Hiding rows basedon values in cells...etc

    OK - can you upload a sample workbook? Click Go Advance, then click the paperclip icon.

  6. #6
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Hiding rows basedon values in cells...etc

    amended code:
    Sub Worksheet_Hide()
    
        Dim lastRow As Long
        Application.Calculation = xlCalculationManual
        lastRow = Range("G65536").End(xlUp).Row
        Rows.Hidden = False
        For Each c In Range("G11:G" & lastRow)
            For Test = 0 To 11
                If c.Offset(0, Test).Value = 0 Then c.EntireRow.Hidden = True
            Next
        Next
    End Sub

  7. #7
    Registered User
    Join Date
    10-05-2011
    Location
    Washington, D.C.
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Hiding rows basedon values in cells...etc

    Here is one worksheet from the book. Both of the codes don't work. The first one just hides two rows, and the "amended code" just hides every row.

    Again as you look at the sheet, I want to hide rows 11 through 62, unless for example row 11 has data other than a 0$, then I don't want the row to collapse.

    This code is the best I have worked up so far:

    Sub Worksheet_Hide()
    
    Dim lastRow As Long, myRange As Range
    
    Application.Calculation = xlCalculationManual
    
    lastRow = Cells(Rows.Count, 2).End(xlUp).Row
    
    Rows.Hidden = False
    
    For Each myRange In Range("r11:r62")
    
    If myRange.Value = 0 Then myRange.EntireRow.Hidden = True
    
    Next myRange
    
    End Sub
    However, for some odd reason it hides row like 15-23 for no reason. I don't get it.

    Thanks.
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Hiding rows basedon values in cells...etc

    A range variable won't be needed. Now that I look at your data, I see that you're using subtotaling and that the totals for each row happen in the T column. Why not hide based on a 0 total in the T column? See code below:
    Sub Worksheet_Hide()
    Application.Calculation = xlCalculationManual
    'following line not needed. Delete if you wish
    'lastRow = Cells(Rows.Count, 2).End(xlUp).Row
    Rows.Hidden = False
    For Each C In Range("T11:T62" & lastRow)
        If C.Value = 0 Then C.EntireRow.Hidden = True
    Next
    'uncomment following line if you want to return to automatic calculation
    'Application.Calculation = xlCalculationAutomatic
    End Sub

  9. #9
    Registered User
    Join Date
    10-05-2011
    Location
    Washington, D.C.
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Hiding rows based on values in cells

    Tlafferty,

    I thought about this already. Here is the issue. The T-column, is a summation of the row, hence TTM (Twelve Month Trailing). However, what if I had an equal positive value in one cell and an equal negative value in the next. So for example, January I have $1000 expense, and in February I have a $ 1000 income. Well the T-column will sum that up and show a $ 0, when in fact that is not true. Because there is data for previous periods, it needs to be analyzed. so that's the issue I am having with analyzing just the T column, and not all of the previous columns. Otherwise it would be a simple fix. Thanks.

  10. #10
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Hiding rows based on values in cells

    OK - this "should" examine each cell one at a time to determine whether or not it contains a zero. If it does, a variable will be incremented. At the end of the data row, a check happens to see if the total number of zeros is 12 for that row. If so, the row is hidden, the variable is reset to 0 for the next row. At the end of the run, it reports in a msgbox which rows were hidden.
    Sub Worksheet_Hide()
        Dim lastRow As Long
        Application.Calculation = xlCalculationManual
        lastRow = Range("G65536").End(xlUp).Row
        Rows.Hidden = False
        For Each c In Range("G11:G" & lastRow)
            For Test = 0 To 11
                If c.Offset(0, Test).Value = 0 Then
                    ValTest = ValTest + 1
                    If ValTest = 12 Then
                        ValTest = 0
                        c.EntireRow.Hidden = True
                        HiddenRow = HiddenRow & c.Row & ", "
                    End If
                End If
            Next
        Next
        MsgBox "Hidden rows: " & HiddenRow
    End Sub

  11. #11
    Registered User
    Join Date
    10-05-2011
    Location
    Washington, D.C.
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Hiding rows based on values in cells

    Quote Originally Posted by tlafferty View Post
    OK - this "should" examine each cell one at a time to determine whether or not it contains a zero. If it does, a variable will be incremented. At the end of the data row, a check happens to see if the total number of zeros is 12 for that row. If so, the row is hidden, the variable is reset to 0 for the next row. At the end of the run, it reports in a msgbox which rows were hidden.
    Sub Worksheet_Hide()
        Dim lastRow As Long
        Application.Calculation = xlCalculationManual
        lastRow = Range("G65536").End(xlUp).Row
        Rows.Hidden = False
        For Each c In Range("G11:G" & lastRow)
            For Test = 0 To 11
                If c.Offset(0, Test).Value = 0 Then
                    ValTest = ValTest + 1
                    If ValTest = 12 Then
                        ValTest = 0
                        c.EntireRow.Hidden = True
                        HiddenRow = HiddenRow & c.Row & ", "
                    End If
                End If
            Next
        Next
        MsgBox "Hidden rows: " & HiddenRow
    End Sub
    Works amazingly. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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