Sorting Columns based on criteria

SimondsJM

New Member
Joined
Apr 16, 2011
Messages
30
Hello,

I am trying to find a way to add clickable sort functions to a sheet, kinda like you have in your typical E-mail.

By Date in Column B, By Alphabet in Column C, By Alphabet in Column D, by Number (Currency) in Column E.

Of course I would like the corresponding data in the row to follow the sort.

Right now I have buttons set as transparent (so you can see whats in the cell) to be used for the sorting function.

So on the sheet "Master List" i have 4 buttons, "CmdDate" will sort by date in Column B, "CmdVendor" will sort alphabetically by vendor name in column C, "CmdCourse" will sort alphabetically by course name in column D, and "CmdCost" will sort numerically (smaller to larger) in Currency format.

Any help is greatly appreciated.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I have used the below Sheet Code to accomplish this. Forget the creation of buttons, just RIGHT-CLICK on the Column Header you want to sort. Good Luck Jim



Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    On Error Resume Next
    If Not Intersect(Target, Range("A1:F1")) Is Nothing Then
    SortCol = ActiveCell.Column
    With ActiveSheet.Range("A1").CurrentRegion
    .Sort Key1:=Target.Value, Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, _
     MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
     End With
    End If
    On Error GoTo 0
End Sub
 
Upvote 0
Instead of buttons try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Static MySortType As Integer
Cancel = True
If MySortType = 0 Then
    MySortType = xlAscending
ElseIf MySortType = xlAscending Then
    MySortType = xlDescending
ElseIf MySortType = xlDescending Then
    MySortType = xlAscending
End If
Target.CurrentRegion.Sort key1:=Target, order1:=MySortType, Header:=xlYes
End Sub

Press ALT + Q to close the code window.

Now, double click in any column to sort by it. Double click the same column again to reverse the sort direction.
 
Upvote 0
It will if you add the lines in red

Rich (BB code):
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Static MySortType As Integer
Cancel = True
Me.Unprotect
If MySortType = 0 Then
    MySortType = xlAscending
ElseIf MySortType = xlAscending Then
    MySortType = xlDescending
ElseIf MySortType = xlDescending Then
    MySortType = xlAscending
End If
Target.CurrentRegion.Sort key1:=Target, order1:=MySortType, Header:=xlYes
Me.Protect
End Sub
 
Upvote 0
Sure, change abc to the password (twice)

Rich (BB code):
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Static MySortType As Integer
Cancel = True
Me.Unprotect Password:="abc"
If MySortType = 0 Then
    MySortType = xlAscending
ElseIf MySortType = xlAscending Then
    MySortType = xlDescending
ElseIf MySortType = xlDescending Then
    MySortType = xlAscending
End If
Target.CurrentRegion.Sort key1:=Target, order1:=MySortType, Header:=xlYes
Me.Protect Password:="abc"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,408
Members
452,912
Latest member
alicemil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top