+ Reply to Thread
Results 1 to 5 of 5

Hiding/collasping a group or named range based on cell value.

  1. #1
    Registered User
    Join Date
    03-10-2015
    Location
    Essex
    MS-Off Ver
    365
    Posts
    20

    Hiding/collasping a group or named range based on cell value.

    Hi experts.

    I'm trying to produce a report that collapses grouping or hides ranges based on a cell value. eg. I have sheet which is made up of pricing elements of a construction job. If an element is priced at £0 then then it doesn't need to appear on the report and I'd like to collapse the group automatically as opposed to clicking each one individually before printing. I have created named ranges on these grouped areas but cont find any code to hide it.

    I've used the following code: (where D21 is the price total and rows 9:20 are the details of the element being priced.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Not Intersect(Target, Range("$d1:d700")) Is Nothing Then

    If Range("d21").Value = 0 Then
    Rows("9:20").EntireRow.Hidden = True
    Else
    Rows("9:20").EntireRow.Hidden = False
    End If

    End If


    End Sub

    This works until i add in an extra row within the range (as will happen when i modify in future) so i need the vba code to collapse the named range rather than the manually specified row numbers. is this possible?

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Hiding/collasping a group or named range based on cell value.

    Example:
    A5: Show
    B5: Value

    B6:B21 has numbers
    This formula, copied down, flags rows to be displayed
    Please Login or Register  to view this content.
    Data Range
    A
    B
    5
    Show
    Value
    6
    TRUE
    1
    7
    TRUE
    2
    8
    FALSE
    0
    9
    TRUE
    3
    10
    TRUE
    5
    11
    FALSE
    0
    12
    TRUE
    7
    13
    FALSE
    0
    14
    TRUE
    8
    15
    FALSE
    0
    16
    FALSE
    0
    17
    FALSE
    0
    18
    FALSE
    0
    19
    FALSE
    0
    20
    FALSE
    0
    21
    FALSE
    0

    Now I use these two macros to show only active rows or to show all rows:
    Please Login or Register  to view this content.


    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Registered User
    Join Date
    03-10-2015
    Location
    Essex
    MS-Off Ver
    365
    Posts
    20

    Re: Hiding/collasping a group or named range based on cell value.

    Hi Ron

    thanks for taking the time to respond. your solution actually answers another little issue i had elsewhere but doesn't address my original question. Its a little more complicated than that.

    eg. my report shows a section for Enabling costs. within this section there are multiple elements that contribute to the total cost of the Enabling works. This is a single cell at the bottom right of the range that references another sheet and if equal to 0 then i want to collapse or hide all of the elements within the range so as to not take up space on the report. What I think i want to do is check the cell value, if its 0 then hide the named range "Range_Enabling".
    see attached picture for a bit more clarity.

    i could just manually click the - handle on the group to the side but there are about 30 of these sections and i'm looking to automate it as much as possible.
    Screenshot (1).png

  4. #4
    Registered User
    Join Date
    03-10-2015
    Location
    Essex
    MS-Off Ver
    365
    Posts
    20

    Re: Hiding/collasping a group or named range based on cell value.

    Hi Guys

    I'm still stuck on this. Referring to above image I'd like to reference cell D21 and if "0" hide my named range "Range_enabling" which is currently rows 9:20. There has to be something out there that will hide the named range rather than the specific rows as by inserting an extra row within 9:20 throws the whole VBA code out and obviously moves my reference cell from D21 to D22.

  5. #5
    Registered User
    Join Date
    03-10-2015
    Location
    Essex
    MS-Off Ver
    365
    Posts
    20

    Re: Hiding/collasping a group or named range based on cell value.

    I've used this code to some effect but its not really what I want to do as it feels a bit clunky.

    Sub HideRows()
    BeginRow = 1
    EndRow = 500
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
    If Cells(RowCnt, ChkCol).Value = 0 Then
    Cells(RowCnt, ChkCol).EntireRow.Hidden = True
    End If
    Next RowCnt
    End Sub

    I just put a 1 in the first column of all the rows I want to never hide and run the macro. Because the amounts within each pricing element reference a separate sheet they either contain an amount or have zero so all cells equal to zero hide the row. It works but I don't like it and would prefer a "cleaner" option if anyone has one.

+ 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. Hiding Named Range based on validation selection on another WS
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-05-2012, 07:01 PM
  2. Hiding Named Range
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-11-2011, 06:37 PM
  3. Hiding a group of columns based on another column
    By fasterthanyours in forum Excel General
    Replies: 1
    Last Post: 01-13-2010, 04:17 PM
  4. Replies: 6
    Last Post: 10-14-2009, 03:01 AM
  5. Replies: 1
    Last Post: 06-03-2006, 10:55 PM

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