How to sort columns by clicking on a cell value?

sigil

New Member
Joined
May 10, 2011
Messages
6
I have several columns of data. I would like to set it up so I can sort it by clicking on one of the cells, or running VBA code that takes the current selected cell's position, does an A-Z sort based on the column of the selected cell, and takes the user to the selected cell's new position in the newly sorted column. How can I do this?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
How come you don't want to use autofilter function provided by excel?
There are more things you could do with it then just sort it.
 
Upvote 0
Because Autofilter doesn't take me to the new position of the cell that was selected before the sort.

Basically, I have column A & column B. If it's sorted by A, so I'm looking at all the values in B that correspond to the same value in A, for any one of those B values I want to be able to see all the values in A that correspond to it.
 
Upvote 0
So you want the columns to be sorted independently from one another?

which means you don't care if the row set of values are mixed up?

and one last question: how many rows of data do you have?
 
Upvote 0
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, x As Variant
Cancel = True
x = Target.Value
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
Columns(Target.Column).Find(x).Select
End Sub

Double click in a column to sort by it. Double click in the same column to reverse the sort.
 
Upvote 0
Try
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim LR As Long, i As Long, j As Long, temp As Variant, val As Variant, c As Long
    
    c = Target.Column
    LRA = Cells(Rows.count, c).End(xlUp).Row
    val = Target.Value
    
    For i = 1 To LR - 1
        For j = i To LR
            If Cells(i, c).Value > Cells(j, c).Value Then
                temp = Cells(i, c).Value
                Cells(i, c).Value = Cells(j, c).Value
                Cells(j, c).Value = temp
            End If
        Next j
    Next i
    
    j = 0
    
    For i = 1 To LRA
        If Cells(i, c).Value = val Then
            Application.Goto reference:=Cells(i, c), scroll:=True
            Exit For
        End If
    Next i
            
    Application.ScreenUpdating = True
End Sub

Double click to sort it.
but dang, VoG's code is always better than mine :( lol learninggg xD
 
Upvote 0
Works perfectly!

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, x As Variant
Cancel = True
x = Target.Value
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
Columns(Target.Column).Find(x).Select
End Sub

Double click in a column to sort by it. Double click in the same column to reverse the sort.
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,189
Members
452,893
Latest member
denay

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