Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA - Toggle between Ascending and Descending Sort

Posted on 2014-01-23
4
Medium Priority
?
634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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.

688 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