Solved

VBA - Toggle between Ascending and Descending Sort

Posted on 2014-01-23
4
597 Views
Last Modified: 2014-01-23
See attached.

Note that if you click on A2, A3,A4 or A5 the sheet sorts ASCENDINGLY left to right.

I need to toggle this from Ascends to Descends (and vice versa).

See my VBA - How do I toggle?
SortLeftToRight.xlsm
0
Comment
Question by:Patrick O'Dea
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
brendanmeyer earned 500 total points
ID: 39805268
update the code to

Option Explicit

Public bsort As Boolean
Public lastrow As Integer

Private Sub Worksheet_BeforedoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim sortzone As Range, thisSheet As Worksheet
    If Target.Column = 1 And Target.Row > 1 Then
        Set thisSheet = ActiveWorkbook.ActiveSheet
        Set sortzone = thisSheet.Range("A1").CurrentRegion.Offset(0, 1)
        Set sortzone = sortzone.Resize(, sortzone.Columns.Count - 1)
        
        thisSheet.sort.SortFields.Clear
        If lastrow = Target.Row Then
            bsort = Not bsort
        Else
            lastrow = Target.Row
            bsort = True
        End If
        If bsort = True Then
            thisSheet.sort.SortFields.Add Key:=Target.Offset(0, 1).Resize(, sortzone.Columns.Count), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        Else
            thisSheet.sort.SortFields.Add Key:=Target.Offset(0, 1).Resize(, sortzone.Columns.Count), _
                SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        End If
        With thisSheet.sort
            .SetRange sortzone
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
        Cancel = True
    End If
    Range("A1").Select
End Sub

Open in new window

0
 

Author Comment

by:Patrick O'Dea
ID: 39805286
One word.... perfect!

Thanks!
0
 
LVL 14

Expert Comment

by:brendanmeyer
ID: 39805307
No Problem, remember to accept the answer :)
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 39805311
Perfect!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question