Adding another column to automatically sort

TJP222

New Member
Joined
Dec 18, 2022
Messages
4
Office Version
  1. 365
  2. 2021
Platform
  1. MacOS
Howdy,

I have this code here that automatically sorts my "Priority" column when a value is changed. I would like it to sort the priority column first and then do a secondary sort by "Due Date." I have fiddled around with it quite a bit and I can't seem to get it to work. Any help is appreciated. Thanks.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim SalesTable As ListObject
Dim SortCol As Range
Set SalesTable = ActiveSheet.ListObjects("ToDo_List")
Set SortCol = Range("ToDo_List[Priority]")
If Not Intersect(Target, SortCol) Is Nothing Then
With SalesTable.sort
.SortFields.Clear
.SortFields.Add Key:=SortCol, Order:=xlAscending, CustomOrder:="High,Medium,Low"
.Header = xlYes
.Apply
End With
End If
End Sub
 

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.
Add another .SortFields.Add line and specify the column number or another range variable as the key. AFAIK, the first sort should be last in the code, the last sort should be first.
If you post code, please use code tags (vba icon on posting toolbar) to maintain indentation and readability.
 
Upvote 0
Add another .SortFields.Add line and specify the column number or another range variable as the key. AFAIK, the first sort should be last in the code, the last sort should be first.
If you post code, please use code tags (vba icon on posting toolbar) to maintain indentation and readability.
I have tried that but I must be doing something wrong. I get "Compile Error: Type Mismatch" and it highlights "C1". Here's the code with indentions.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim SalesTable As ListObject
Dim SortCol As Range
Set SalesTable = ActiveSheet.ListObjects("ToDo_List")
Set SortCol = Range("ToDo_List[Priority]")
If Not Intersect(Target, SortCol) Is Nothing Then
With SalesTable.sort
.SortFields.Clear
.SortFields.Add Key:="C1", Order:=xlAscending
.SortFields.Add Key:=SortCol, Order:=xlAscending, CustomOrder:="High,Medium,Low"
.Header = xlYes
.Apply
End With
End If
End Sub
 
Upvote 0
I have tried that but I must be doing something wrong. I get "Compile Error: Type Mismatch" and it highlights "C1". Here's the code with indentions.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim SalesTable As ListObject
Dim SortCol As Range
Set SalesTable = ActiveSheet.ListObjects("ToDo_List")
Set SortCol = Range("ToDo_List[Priority]")
If Not Intersect(Target, SortCol) Is Nothing Then
With SalesTable.sort
.SortFields.Clear
.SortFields.Add Key:="C1", Order:=xlAscending
.SortFields.Add Key:=SortCol, Order:=xlAscending, CustomOrder:="High,Medium,Low"
.Header = xlYes
.Apply
End With
End If
End Sub
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim SalesTable As ListObject
Dim SortCol As Range
Set SalesTable = ActiveSheet.ListObjects("ToDo_List")
Set SortCol = Range("ToDo_List[Priority]")
If Not Intersect(Target, SortCol) Is Nothing Then
    With SalesTable.sort
        .SortFields.Clear
        .SortFields.Add Key:="C1", Order:=xlAscending
        .SortFields.Add Key:=SortCol, Order:=xlAscending, CustomOrder:="High,Medium,Low"
        .Header = xlYes
        .Apply
    End With
End If
End Sub
 
Upvote 0
It doesn't know what C1 is. It's expecting a range and C1 is just text. Did you try
Key:=Range("C1")? You might have to qualify that with ActiveSheet. If so, that should be safe enough as the active sheet should always be the one the code is running against since you're using Worksheet_Change (as long as you don't activate a different sheet during the event).
 
Upvote 0
Solution
It doesn't know what C1 is. It's expecting a range and C1 is just text. Did you try
Key:=Range("C1")? You might have to qualify that with ActiveSheet. If so, that should be safe enough as the active sheet should always be the one the code is running against since you're using Worksheet_Change (as long as you don't activate a different sheet during the event).
Thanks Micron
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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