How can I edit this sort so it work when a set of cells is clicked not just1?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,210
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone

The code below works great,
what it does is when I click on cell AJ101
it sorts my data using AJ as the sort column.

Now I want to be able to do this with all the headers in the range, so when I click AC101 it sort by AC ect using the same rules as I currently use.

So how can I change this so it says If target is within Range AB101:AO101 (instead of (If Target.Address = "$AJ$101" Then))
Then sort
and
SortCol ="selected cell" instead of (SortCol = "AJ") ??

any ideas?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$AJ$101" Then
Sheets("Dashboard1").Unprotect
SortCol = "AJ"
Lrow = Sheets("Dashboard1").Cells(Rows.Count, "AC").End(xlUp).Row
If Lrow < 102 Then
Lrow = 102
End If
Sheets("Dashboard1").Range("AB101", "AO" & Lrow).Sort Key1:=Range(SortCol & "102", SortCol & Lrow), Header:=xlYes, Order1:=xlDescending
Sheets("Dashboard1").Protect
Exit Sub
End If

Thanks

Tony
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi,
not tested but have a play with this updated code & see if helps you

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim SortCol As String
    Dim Lrow As Long
    
    If Not Intersect(Target, Me.Range("AB101:AO101")) Is Nothing Then
        Me.Unprotect
        SortCol = Left(Target.Address(0, 0), 2)
        Lrow = Me.Cells(Rows.Count, "AC").End(xlUp).Row
        If Lrow < 102 Then Lrow = 102
        
        Me.Range("AB101", "AO" & Lrow).Sort Key1:=Range(SortCol & 102, SortCol & Lrow), Header:=xlYes, Order1:=xlDescending
        Me.Protect
    End If
End Sub

I have assumed that Sheets("Dashboard1") is the activesheet.

Hope Helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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