+ Reply to Thread
Results 1 to 4 of 4

Toggle Sort by selecting column (row, not header) or cell in table

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Toggle Sort by selecting column (row, not header) or cell in table

    Hi, I am trying to figure out how to sort a table column by descending order [or ascending order if its already in descending order] by clicking either the column "Header" (my actual header is a hidden row because the names are not user friendly, so the user will see the row above the table and this is what they will be double clicking. The caveat here is that the table has columns with number, alpha-numberic, all alpha, currency, and dates... does anyone know how to accomplish this? I could also link a (Click) button to each cell header if that is easier but am trying to avoid that because then will need to edit each macro according to the data in that particular column. The example workbook attached is only some of the columns... in the actual workbook its around 70 columns with 10-15k rows so trying to keep this as lightweight as possible

    Thanks for any help!
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Toggle Sort by selecting column (row, not header) or cell in table

    Hello BG1983,

    If I understood you needs correctly then the attached workbook should do what you want.

    Here is the code that was added...

    Sheet1 Selection Change Event Code
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Cells.Count > 1 Then Exit Sub
        
        If Not Intersect(Target, ActiveSheet.ListObjects(1).HeaderRowRange.Offset(-1, 0)) Is Nothing Then
            Call SortColumn
        End If
        
    End Sub
    Module1 Code
    Sub SortColumn()
    
        Dim Cell        As Range
        Dim Rng         As Range
        Dim sortOrder   As Long
        Dim Tbl         As ListObject
            
            On Error Resume Next
                Set Tbl = ActiveSheet.ListObjects(1)
                If Err <> 0 Then
                    MsgBox "There is no Table on this worksheet.", vbExclamation
                    Exit Sub
                End If
            On Error GoTo 0
                    
            Set Cell = Tbl.Range.Columns(ActiveCell.Column - Tbl.Range.Column + 1).Cells(1, 1)
                            
            With Tbl.Sort
                If .SortFields.Count > 0 Then
                    sortOrder = .SortFields(1).Order
                    If sortOrder = 1 Then
                        sortOrder = 2
                    Else
                        sortOrder = 1
                    End If
                Else
                    sortOrder = xlAscending
                End If
                .SortFields.Clear
                .SortFields.Add Key:=Cell, Order:=sortOrder, SortOn:=xlSortOnValues, DataOption:=xlSortNormal
                .Header = Yes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .Apply
            End With
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Toggle Sort by selecting column (row, not header) or cell in table

    Awesome! Very much appreciated!

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Toggle Sort by selecting column (row, not header) or cell in table

    Hello BG1983,

    You're welcome. It is always nice to get it right the first time.

+ 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. Find Row OR Column Header for a cell in a two way table WITHOUT VBA
    By militiamc in forum Excel Formulas & Functions
    Replies: 19
    Last Post: 03-31-2015, 02:23 AM
  2. Replies: 0
    Last Post: 02-16-2015, 04:54 PM
  3. [SOLVED] Find specific value in table and return header row and header column value
    By nelwan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-30-2013, 01:35 AM
  4. [SOLVED] Extract the column header given the row header and highest value in a subset of a table
    By pguarino in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-20-2013, 02:05 AM
  5. Selecting a column range based on a value in header row
    By Ad83 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-05-2012, 05:29 PM
  6. Replies: 3
    Last Post: 05-17-2009, 04:31 AM
  7. Addressing Table cell using column header
    By munzer1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2007, 03:14 AM

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