+ Reply to Thread
Results 1 to 20 of 20

VBA Code to Sort a list with column header activating the code

  1. #1
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    VBA Code to Sort a list with column header activating the code

    I have a problem that I think is best solved with VBA code. I have a table (attached) that I want to sort by clicking the Year Date at the top of each column B6 through G6. I found this article https://trumpexcel.com/sort-data-vba/ but I can get it to do any thing adapting this code.

    I have provide the results corrected sort from Largest to Smallest in Columns I through M with the cell I want to activate the code on a "click" in green just for clarity but the real table will have the dates all the same color. I also what to sort the "Unit" B column alphabetically.
    Attached Files Attached Files
    Last edited by Bobbbo; 11-04-2019 at 05:20 PM. Reason: Solved

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: VBA Code to Sort a list with column header activating the code

    .
    The following will do what you are seeking EXCEPT for placing the MEDIAN row at the bottom of the table. Perhaps you can start with this
    and determine the MEDIAN ROW issue after.

    Please Login or Register  to view this content.
    https://www.extendoffice.com/documen...r-to-sort.html

  3. #3
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: VBA Code to Sort a list with column header activating the code

    Thanks for the help, this solution allows you to sort on any cell, which can make a real mess out of the whole table. And median cannot be included in the sort.

  4. #4
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: VBA Code to Sort a list with column header activating the code

    Hi Logit, I found a solution to the "Median" row not being included in the sort. I I insert a row before the "Median" row, then hide the row. Median is no longer included in the sort.
    Last edited by Bobbbo; 11-03-2019 at 12:39 PM. Reason: typo

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: VBA Code to Sort a list with column header activating the code

    .
    Glad you found the answer.

    If that solves your original question please mark this thread as SOLVED.

  6. #6
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: VBA Code to Sort a list with column header activating the code

    Actually it does not as every cell can be sorted. Is there a way to restrict it to the header row only having the double click feature and no others?

  7. #7
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: VBA Code to Sort a list with column header activating the code

    .
    Ok .. try this. It works here ...

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this ‼


    According to your attachment as a beginner starter :

    PHP Code: 
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As RangeCancel As Boolean)
        If 
    Not Intersect([B6:G6], TargetIs Nothing Then
            Cancel 
    True
            
    [B6:G6].Interior.Color vbYellow
            
    [B7:G13].Sort Target(2), xlDescending + (Target.Column 2), Header:=xlNo
            
    If Target.Column 2 Then Target.Interior.Color vbGreen
        End 
    If
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Variation if …


    … the table is dynamic :

    PHP Code: 
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As RangeCancel As Boolean)
        
    With [B6].CurrentRegion.Rows
            
    If Not Intersect(.Item(1), TargetIs Nothing Then
                Cancel 
    True
               
    .Item(1).Interior.Color vbYellow
               
    .Item("2:" & .Count 1).Sort Target(2), + (Target.Column = .Column), Header:=xlNo
                
    If Target.Column > .Column Then Target.Interior.Color vbGreen
            End 
    If
        
    End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  10. #10
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: VBA Code to Sort a list with column header activating the code

    Both Logit and Mar L,

    Both your solutions work! Thanks so much!! How can I adapt either solution to sort the table in B6:G6 and also have the ability to sort additional tables such as I18:N18 as the actual project has multiple tables. I know I only asked for the B6:G6 to keep the question simple per the forums request, but I do need this additional twist of sorting multiple (2 or more) tables on the same Spreadsheet.

    Thanks again!

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Thanks for the rep' !

    In this case if you convert your ranges to real Excel tables it would be easier to detect
    if any double click is within any headers row of any Excel table !

    No need to convert if the worksheet contains only 'tables', of course attach a new sample workbook …

    Do you prefer a green background or just a bold font for the sort header ?

  12. #12
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: VBA Code to Sort a list with column header activating the code

    Quote Originally Posted by Marc L View Post
    Thanks for the rep' !

    In this case if you convert your ranges to real Excel tables it would be easier to detect
    if any double click is within any headers row of any Excel table !

    No need to convert if the worksheet contains only 'tables', of course attach a new sample workbook …

    Do you prefer a green background or just a bold font for the sort header ?
    I am not sure by your statement "real Excel Tables"? Actually I do like to colors, but Bold font would work well also.

    Thanks, Bobbbo

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    In your initial attachment there is no table, just ranges …
    Attach at least a new sample workbook according to your new need.

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb An idea …

    (removed as not a good idea)
    Last edited by Marc L; 11-04-2019 at 12:59 PM. Reason: optimization 'cause of blank row before median …

  15. #15
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: VBA Code to Sort a list with column header activating the code

    Great, now I understand. I have attached a sample closer to my final project. In the attached sample I need to sort on header Row 6 and Header Row 20. I added a blank ROW 15 and 29 to separate the median so it is not included in the sort and "Hide" that row.

    If I convert these to "Tables" it automatically changes the header names in columns G through R because I used the same column names under each Metric which a table will not allow. That was the main reason I was looking for VBA code to do that sorting rather than with a Real Excel Table.
    Attached Files Attached Files

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb

    According to your last attachment (edit) :

    PHP Code: 
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As RangeCancel As Boolean)
        
    With Target.CurrentRegion.Rows
            
    If Not Intersect(.Item(2), TargetIs Nothing Then
                Cancel 
    True
               
    .Item(2).Font.Bold False
               
    .Item("3:" & .Count).Sort Target(2), + (Target.Column = .Column), Header:=xlNo
                Target
    .Font.Bold True
            End 
    If
        
    End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 11-04-2019 at 12:57 PM.

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    See the code just edited in my previous post …

  18. #18
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: VBA Code to Sort a list with column header activating the code

    Hi Marc, WOW it works great. Is there any way to have it toggle from descending to ascending as some of the metrics small is better and other larger is better.

  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb

    A toggle starter :

    PHP Code: 
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As RangeCancel As Boolean)
            
    Dim B As Boolean
        With Target
    .CurrentRegion.Rows
            
    If Not Intersect(.Item(2), TargetIs Nothing Then
                Cancel 
    True
                B 
    Target.Font.Bold
               
    .Item(2).Font.Bold False
               
    .Item(2).Interior.ColorIndex xlColorIndexNone
               
    .Item("3:" & .Count).Sort Target(2), BHeader:=xlNo
                Target
    .Font.Bold Not B
                
    If B Then Target.Interior.Color vbYellow
            End 
    If
        
    End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  20. #20
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: VBA Code to Sort a list with column header activating the code

    Hi Marc, This code WORKS. Thanks so much. I will mark this solved. Great job.

+ 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. Activating in cell drop down list creates a VBA code execution error
    By VBA Grunt in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-03-2021, 03:27 AM
  2. [SOLVED] VBA code to sort a list by WBS code
    By LeanAccountant in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-20-2018, 10:58 AM
  3. Big List Sort. Not Enough Memory. Help Run my Code. Or help fix or change code Please :)
    By Doc.AElstein in forum Excel Programming / VBA / Macros
    Replies: 47
    Last Post: 06-08-2017, 06:56 AM
  4. VB Code Custom Sort then Insert Header
    By Intalzky in forum Excel General
    Replies: 3
    Last Post: 09-16-2014, 11:06 PM
  5. Sort column list by header
    By suprchic66 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-03-2014, 11:06 PM
  6. [SOLVED] Use the column Header to column letter code to set range in Macro
    By capson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-26-2013, 11:06 AM
  7. Code to Sort Data by Clicking on Header Row Cells
    By Coop913 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-07-2009, 01:24 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