Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBA - Toggle between Ascending and Descending Sort

Posted on 2014-01-23
4
Medium Priority
?
643 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

916 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