Results 1 to 10 of 10

Thread: Sort Data When a Header Is Clicked

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Remove ALL the macros in both the sheet, as well as in the modules. Copy the below code to ThisWorkbook module. You can now add any number of sheets, and the right-click controls will be available.

    Also, the headerRow will now be available in the entire module, and just need to pass value to it in the AddOnRightClick() routine

    Code:
    Option Explicit
    
    Dim HeaderRow As Long
    
    Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
        
        AddOnRightClick
        
    End Sub
    
    Private Sub DeleteOnRightClick()
    
        On Error Resume Next
        With Application
            .CommandBars("Cell").Controls("Sort Descending").Delete
            .CommandBars("Cell").Controls("Sort Ascending").Delete
        End With
        On Error GoTo 0
        
    End Sub
    Private Sub AddOnRightClick()
    
        On Error Resume Next
        HeaderRow = 4 'How to pass the variable to the sub sorting the dat
        Dim SortAsceButton As CommandBarButton
        Dim SortDescButton As CommandBarButton
        With Application
            .CommandBars("Cell").Controls("Sort Descending").Delete
            Set SortDescButton = .CommandBars("Cell").Controls.Add(Temporary:=True, Before:=1)
        End With
        With Application
            .CommandBars("Cell").Controls("Sort Ascending").Delete
            Set SortAsceButton = .CommandBars("Cell").Controls.Add(Temporary:=True, Before:=1)
        End With
        With SortAsceButton
           .BeginGroup = True
           .Style = msoButtonIconAndCaption
           .Caption = "Sort Ascending"
           .FaceId = 125
           .OnAction = "ThisWorkbook.SortAscending"
        End With
        With SortDescButton
           .BeginGroup = True
           .Style = msoButtonIconAndCaption
           .Caption = "Sort Descending"
           .FaceId = 125
           .OnAction = "ThisWorkbook.SortDesc"
        End With
        Set SortAsceButton = Nothing
        Set SortDescButton = Nothing
        On Error GoTo 0
        
    End Sub
    
    Sub SortDesc()
    
        Select Case ActiveCell.Row
            Case HeaderRow
                If IsEmpty(ActiveCell.Value) Then Exit Sub
                Static MySortType As Integer
                MySortType = xlDescending
                'Target.CurrentRegion.Offset(1).Sort key1:=Target, order1:=MySortType, Header:=xlYes
                ActiveCell.CurrentRegion.Offset(0).Sort key1:=ActiveCell, order1:=MySortType, Header:=xlYes
                On Error Resume Next
                Err.Clear
        End Select
        
    End Sub
    
    Sub SortAscending()
    
        Select Case ActiveCell.Row
            Case HeaderRow
                If IsEmpty(ActiveCell.Value) Then Exit Sub
                Static MySortType As Integer
                MySortType = xlAscending
                'Target.CurrentRegion.Offset(1).Sort key1:=Target, order1:=MySortType, Header:=xlYes
                ActiveCell.CurrentRegion.Offset(0).Sort key1:=ActiveCell, order1:=MySortType, Header:=xlYes
                On Error Resume Next
                Err.Clear
        End Select
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    15
    Perfect - exactly what the Doctor ordered - Thanks

    Also use FaceId 3157 & 3158 - symbols for sorting

    I uploaded the final XLSM files under tips-Tricks and downloads - this is a very usefull tool - Excel Fox & Admin - thanks for your help.
    Last edited by Rasm; 10-29-2011 at 06:25 PM.
    xl2007 - Windows 7
    xl hates the 255 number

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Experience Vba co- New to this terrfic site - but cannot duplicate the sort procedure

    Hello Folks,
    Maybe I am just having first time hickups to the site but where is data passed for the sort?

    Or I am missing a point on this macro?

  4. #4
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    15
    Fioramonti

    This routine sorts the entire sheet from the headerrow down -- are you looking to only sort a single column of data or a range of cells?

    Rasm
    xl2007 - Windows 7
    xl hates the 255 number

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Sorting in range

    Hello frasm,

    Yes, I would be sorting a range of rows, single column.

    Thanks

    fioramonti

Similar Threads

  1. Sort Data Using Formula To Find Top X
    By mahmoud-lee in forum Excel Help
    Replies: 12
    Last Post: 06-02-2013, 10:13 PM
  2. Replies: 6
    Last Post: 05-10-2013, 01:13 AM
  3. Sort data sheet by right_click of mouse
    By Rasm in forum Excel and VBA Tips and Tricks
    Replies: 3
    Last Post: 12-08-2012, 07:34 PM
  4. Meger multiple file but header not same
    By rocky in forum Excel Help
    Replies: 14
    Last Post: 10-25-2012, 09:09 PM
  5. Excel Macro to Sort Data if a value changes in defined range
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 4
    Last Post: 09-05-2012, 10:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •