+ Reply to Thread
Results 1 to 14 of 14

Hide Rows Depending on Values in Column

Hybrid View

  1. #1
    Registered User
    Join Date
    03-20-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Hide Rows Depending on Values in Column

    Hi Everyone,

    How can I hide rows depending on the value in a column. I have a spreadsheet where column A is calculated as the sum of columns B to G. If this value is 0 I want to hide the row. From searching online I have found the following but cant get it to work:

    ---------------------------

    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Not Intersect(Target, Range("A2:A100")) Is Nothing Then
    If Target.Value = 0 Then Target.EntireRow.Hidden = True
    End If
    End Sub

    ---------------------------

    As a complete beginner to Macros where do I put this? I have tried right clicking the Tab, selecting view code, and pasting it into the screen. This hasn't worked. Do I need to do something to activate or run it? Can the code be set up so it constantly runs, so whenever a value on the sheet is changed rows will automatically be hidden or unhidden?

    Thanks,

    Ger
    Last edited by gerainta; 05-15-2012 at 06:40 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: Hide Rows Depending on Values in Column

    Use an auto filter

  3. #3
    Registered User
    Join Date
    03-20-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Hide Rows Depending on Values in Column

    An auto filter doesn't work due to the structure of the spreadsheet. If I filter this small section of data I can't put filters in place to filter the main bulk of data further down.

    I would also like the update to be automatic. A lot of people will use this sheet and I'm sure some of them will forget to manually refresh an autofilter giving them incorret data.

  4. #4
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Hide Rows Depending on Values in Column

    Event macros need to be placed in the module that corresponds to the object they refer to. In the case of the code you pasted, it will need to go on the "worksheet" module that corresponds to the sheet you want it to work in. To do this:

    Open VBA screen (Alt+F11)
    Click on the + next to VBAProject(YourWorkbookName)
    Double click on Sheet#(YourSheetName)
    Paste your code there.

    I assume you understand that the code you pasted will check every time a cell is changed and will hide all the rows according to the value, but only if what is changed is in column A and the value is 0. What I mean is it won't check the entire sheet, only the row that was changed. And only if the changed happened on column A.
    Last edited by Pichingualas; 05-15-2012 at 08:44 AM. Reason: line added in blue
    .?*??)
    `?.???.?*??)?.?*?)
    (?.?? (?.?
    Pichingualas <---
    ??????????????????????????

    Wrap your code with CODE TAGS.
    Thank those who helped you, Don't forget to add to their REPUTATION!!! (click on the star below their post).
    Please mark your threads as [SOLVED] when they are (Thread Tools->Mark thread as Solved).

  5. #5
    Registered User
    Join Date
    03-20-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Hide Rows Depending on Values in Column

    Thanks Pichingualas,

    I have now got it working as you described. I think in this case the code I have found doesn't work for what I want it to do.

    I have attached an example workbook showing the type of problem I have. I want to hide any rows where the control is 0. The control is calculated from sheet 2 and so will never be changed. Whenever the sheet is updated it will either be by changing the numbers in the data columns or by adding extra rows. When the data is modified again so that the control is no longer z I need the row to come back.

    How can I do this?

    Thanks,

    Ger

    HideRowsTest.xlsx

  6. #6
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Hide Rows Depending on Values in Column

    Try with this code:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Ws As Worksheet
        Dim RowCheck As Long
        
        Set Ws = ActiveSheet
        
        Application.ScreenUpdating = False
        With Ws
            For RowCheck = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
                If .Cells(RowCheck, 1) = 0 Then
                    .Cells(RowCheck, 1).EntireRow.Hidden = True
                Else
                    .Cells(RowCheck, 1).EntireRow.Hidden = False
                End If
            Next RowCheck
        End With
        Application.ScreenUpdating = True
    End Sub
    Just a note: this code will run with ANY change made on your sheet. If you have a lot of information on it, it might make your machine slow. I would advice to place a button with a macro attached to it so that the user clicks on it before using the data on the sheet, instead of using an event macro like this. Of course, that is my point of view and you might think you want it like this, I just wanted to tell you beforehand so you don't see any surprises.

  7. #7
    Registered User
    Join Date
    03-20-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Hide Rows Depending on Values in Column

    Thats a lot better already, however the changes are all made on sheet 2, and the filter is applied to sheet 1. As the sheet 1 tab needs to be clicked to view the changes after they have been made on a different sheet is it possible to run the code when the sheet 1 tab is clicked instead? This should stop it from slowing?

  8. #8
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Hide Rows Depending on Values in Column

    That will help, yes. Change the macro to this if that's what you want:
    Option Explicit
    
    Private Sub Worksheet_Activate()
        Dim Ws As Worksheet
        Dim RowCheck As Long
        
        Set Ws = ActiveSheet
        
        Application.ScreenUpdating = False
        With Ws
            For RowCheck = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
                If .Cells(RowCheck, 1) = 0 Then
                    .Cells(RowCheck, 1).EntireRow.Hidden = True
                Else
                    .Cells(RowCheck, 1).EntireRow.Hidden = False
                End If
            Next RowCheck
        End With
        Application.ScreenUpdating = True
    End Sub

  9. #9
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: Hide Rows Depending on Values in Column

    Thanks to all for the help.

  10. #10
    Registered User
    Join Date
    03-20-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Hide Rows Depending on Values in Column

    Thanks for all your help.

    One last thing. If I want to run the macro by changing a particular cell as well as by clicking the tab, how can I do that? I think it would be something like the following if C3 is the cell I want to edit, but this doesn't work.

    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As "C3")
    Private Sub Worksheet_Activate()
    Dim Ws As Worksheet
    Dim RowCheck As Long

    Set Ws = ActiveSheet

    Application.ScreenUpdating = False
    With Ws
    For RowCheck = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
    If .Cells(RowCheck, 1) = 0 Then
    .Cells(RowCheck, 1).EntireRow.Hidden = True
    Else
    .Cells(RowCheck, 1).EntireRow.Hidden = False
    End If
    Next RowCheck
    End With
    Application.ScreenUpdating = True
    End Sub


    Thanks again,

    Ger

  11. #11
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Hide Rows Depending on Values in Column

    No that won't work, you need to have two separate macros for it. You would have to add something like this:

    Option Explicit
    
    Private Sub Worksheet_Activate()
        Dim Ws As Worksheet
        Dim RowCheck As Long
        
        Set Ws = ActiveSheet
        
        Application.ScreenUpdating = False
        With Ws
            For RowCheck = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
                If .Cells(RowCheck, 1) = 0 Then
                    .Cells(RowCheck, 1).EntireRow.Hidden = True
                Else
                    .Cells(RowCheck, 1).EntireRow.Hidden = False
                End If
            Next RowCheck
        End With
        Application.ScreenUpdating = True
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Ws As Worksheet
        Dim RowCheck As Long
        
        Set Ws = ActiveSheet
        If Not Intersect(Target, Range("C3")) Is Nothing Then
            Application.ScreenUpdating = False
            With Ws
                For RowCheck = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
                    If .Cells(RowCheck, 1) = 0 Then
                        .Cells(RowCheck, 1).EntireRow.Hidden = True
                    Else
                        .Cells(RowCheck, 1).EntireRow.Hidden = False
                    End If
                Next RowCheck
            End With
            Application.ScreenUpdating = True
        End If
    End Sub
    Since there are two different events involved, you need two separate macros for it. Each event macro works for only one event. (event is the action that triggers it "select the tab", in fact activate the sheet, or "change C3")
    Last edited by Pichingualas; 05-15-2012 at 11:51 AM.

  12. #12
    Registered User
    Join Date
    03-20-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Hide Rows Depending on Values in Column

    Hi Pichingualas,

    With my sheet so far I am having problems with updates made on the same page. I have attached an example file to illustrate the problem. Row 2 on the results tab is calculated from row 2 on the data tab. When the result is 0 it hides and when the result is not it comes back.

    Row 3 on the results tab is calculated from row 3 on the same tab. This time when the sum is 0 the row hides but when the sum is not 0 it remains hidden. Having gone through the code I can't see any piece of code to distinguish where the data comes from, or am I missing something obvious?

    By the way, thanks so much for your help, I have learned so much since yesterday morning.

    HideRowsTest.xlsm

  13. #13
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Hide Rows Depending on Values in Column

    Make this change:

    Option Explicit
    
    Private Sub Worksheet_Activate()
        Dim Ws As Worksheet
        Dim RowCheck As Long
        Set Ws = ActiveSheet
        Application.ScreenUpdating = False
        With Ws
            For RowCheck = 1 To .UsedRange.Rows.Count
                If .Cells(RowCheck, 1).Value = 0 And .Cells(RowCheck, 1).Value <> "" Then
                    .Cells(RowCheck, 1).EntireRow.Hidden = True
                Else
                    .Cells(RowCheck, 1).EntireRow.Hidden = False
                End If
            Next RowCheck
        End With
        Application.ScreenUpdating = True
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Ws As Worksheet
        Dim RowCheck As Long
        
        Set Ws = ActiveSheet
        If Not Intersect(Target, Range("C1")) Is Nothing Then
            Application.ScreenUpdating = False
            With Ws
                For RowCheck = 1 To .UsedRange.Rows.Count
                    If .Cells(RowCheck, 1).Value = 0 And .Cells(RowCheck, 1).Value <> "" Then
                        .Cells(RowCheck, 1).EntireRow.Hidden = True
                    Else
                        .Cells(RowCheck, 1).EntireRow.Hidden = False
                    End If
                Next RowCheck
            End With
            Application.ScreenUpdating = True
        End If
    End Sub
    The problem was that apparently End(xlUp), which is intended to find the last non empty cell, will only look at cells that are not hidden, so if the one you intended to show is the last on your list it wouldn't see it. I didn't know that lol. Also I added a condition, that the cell is not empty when hiding, because before that it was seeing empty cells as being equal to 0. So I changed both macros. (I marked the changes made in blue)

  14. #14
    Registered User
    Join Date
    03-20-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Hide Rows Depending on Values in Column

    Pichingualas,

    Thank you so much for all the help! Thats fantastic

    Ger

+ 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