Solved

VBA - Toggle between Ascending and Descending Sort

Posted on 2014-01-23
4
583 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now