+ Reply to Thread
Results 1 to 3 of 3

Double click to colapse a set number of rows

Hybrid View

  1. #1
    Registered User
    Join Date
    03-09-2014
    Location
    North Carolina
    MS-Off Ver
    Excel 2013
    Posts
    24

    Double click to colapse a set number of rows

    Hello,
    I am trying to build a worksheet which has an account number in column B with several more rows of content starting in column C and beyond. I would like to be able to hide the 9 rows under the row with the account number by double clicking on the account number. I found this code which will work by collapsing to the next account number, but it collapses the account number underneath it too. Is there a way to collapse only the blank cells in the column until the next number or to program the code to only collapse/hide the next 9 rows below the double clicked cell?

    Option Explicit
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim i As Long
    i = Range("B" & Rows.Count).End(xlUp).Row 
    If Target.Column = 2 Then
        Cancel = True
        If Range("B" & Target.Row + 1).Rows.Hidden Then
            Range("B:B").Rows.Hidden = False
        Else
            Range("B" & Target.Row + 1 & ":B" & i).Rows.Hidden = True
        End If
    End If
    End Sub
    Thanks All!

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,946

    Re: Double click to colapse a set number of rows

    Take a look at using Grouping, on the Data tab. You may be able to do this without using VBA
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    03-09-2014
    Location
    North Carolina
    MS-Off Ver
    Excel 2013
    Posts
    24

    Re: Double click to colapse a set number of rows

    I would prefer to use a double-click function. I am using this code to both hide the empty cells between two cells with 'account numbers' and to double-click some color in other cells.
    Option Explicit
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim HideRows As Boolean
        
        If Target.Column >= 4 Then
            Cancel = True
            Select Case Target.Interior.ColorIndex
                Case xlNone, 4: Target.Interior.ColorIndex = 3
                Case Else: Target.Interior.ColorIndex = 4
            End Select
        End If
        
        If Application.Intersect(Target, Range("B:B")) Is Nothing Then Cancel = True: Exit Sub
        
        If Rows(Target.Offset(1, 0).Row).Hidden Then _
            HideRows = False Else HideRows = True
        'if there is no cell with a value below the clicked cell then cancel
        If Cells.Rows.Count = Target.End(xlDown).Row Then Cancel = True _
                Else Range(Target.Offset(1, 0), Target.End(xlDown).Offset(-1, 0)).Rows.Hidden = HideRows
        
    End Sub
    The problem is that this VBA opens all the hidden cells below it instead of just the cell clicked. Any ideas?

+ 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. Changing cell properties with double click, then revert with another double click
    By mweber2525 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-01-2014, 01:40 PM
  2. Copy double-clicked cell to next empty row in different sheet with each double-click
    By erhathaway in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-25-2013, 12:06 AM
  3. Any way to double click on a row number and have it copy that row to the clip board?
    By Albertsonsmowing in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-16-2012, 11:14 AM
  4. [SOLVED] userform label double-click goes to click event
    By John Paul Fullerton in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-19-2006, 01:00 PM
  5. Replies: 4
    Last Post: 08-02-2005, 09:05 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