Custom Sorting VBA

eli_m

Board Regular
Joined
Jun 2, 2022
Messages
153
Office Version
  1. 365
Platform
  1. Windows
Hi,

I manually have my custom sorting as:
Column B (Surgery Date) - Oldest to Newest
then
Column F (Surgeon) - A to Z

1716528238592.png



Currently, I have the code below to sort by the Surgery Date by Oldest to Newest which has been working great but how do I add Column F (Surgeon) - A to Z as the secondary sort?

This is my current code:
VBA Code:
Option Explicit
Private Sub Workbook_Open()

    If ActiveSheet.Name = Sheet1.Name Then
    'Start Timeout Timer
        checktime = False
        Call checktimer
    'Check Timeout timer
        checktime = True
        Lastchange = Now()
     End If
  
  
  Dim cel As Range
 
  Application.ScreenUpdating = False
  ThisWorkbook.RefreshAll
  With Sheets("IPS Cases")
    On Error Resume Next
    ThisWorkbook.Sheets("IPS Cases").Range("B2").Sort Key1:=ThisWorkbook.Sheets("IPS Cases").Range("B3"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
    On Error GoTo 0
    For Each cel In .Range("A2:BP200")
      With cel
        .Errors(8).Ignore = True
        .Errors(9).Ignore = True
        .Errors(6).Ignore = True
      End With
    Next cel
  End With
  Application.ScreenUpdating = True
End Sub

Thank you in advance :)
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
In this line
VBA Code:
ThisWorkbook.Sheets("IPS Cases").Range("B2").Sort Key1:=ThisWorkbook.Sheets("IPS Cases").Range("B3"), _
Order1:=xlAscending, Header:=xlYes, _
add Key2:= and Order2:= after Order1
 
Upvote 0
Solution
In this line
VBA Code:
ThisWorkbook.Sheets("IPS Cases").Range("B2").Sort Key1:=ThisWorkbook.Sheets("IPS Cases").Range("B3"), _
Order1:=xlAscending, Header:=xlYes, _
add Key2:= and Order2:= after Order1
Thanks! I think this did the trick:

VBA Code:
Option Explicit
Private Sub Workbook_Open()

    If ActiveSheet.Name = Sheet1.Name Then
    'Start Timeout Timer
        checktime = False
        Call checktimer
    'Check Timeout timer
        checktime = True
        Lastchange = Now()
     End If
   
   
  Dim cel As Range
 
  Application.ScreenUpdating = False
  ThisWorkbook.RefreshAll
  With Sheets("IPS Cases")
    On Error Resume Next
    ThisWorkbook.Sheets("IPS Cases").Range("B2").Sort Key1:=ThisWorkbook.Sheets("IPS Cases").Range("B3"), _
Order1:=xlAscending, Header:=xlYes, _
Key2:=ThisWorkbook.Sheets("IPS Cases").Range("B3"), Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
    On Error GoTo 0
    
    For Each cel In .Range("A2:AR200") 'Ignore ALL Errors
      With cel
        .Errors(6).Ignore = True 'Lock Error
        .Errors(7).Ignore = True 'Unprotected Forumla Error
        .Errors(8).Ignore = True 'Data Validation Error
        .Errors(9).Ignore = True 'Inconsistent Error
        .Errors(10).Ignore = True 'Inconsistent Calculated Column Formula Error
      End With
      
    Next cel
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks! I think this did the trick:

VBA Code:
Option Explicit
Private Sub Workbook_Open()

    If ActiveSheet.Name = Sheet1.Name Then
    'Start Timeout Timer
        checktime = False
        Call checktimer
    'Check Timeout timer
        checktime = True
        Lastchange = Now()
     End If
  
  
  Dim cel As Range
 
  Application.ScreenUpdating = False
  ThisWorkbook.RefreshAll
  With Sheets("IPS Cases")
    On Error Resume Next
    ThisWorkbook.Sheets("IPS Cases").Range("B2").Sort Key1:=ThisWorkbook.Sheets("IPS Cases").Range("B3"), _
Order1:=xlAscending, Header:=xlYes, _
Key2:=ThisWorkbook.Sheets("IPS Cases").Range("B3"), Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
    On Error GoTo 0
   
    For Each cel In .Range("A2:AR200") 'Ignore ALL Errors
      With cel
        .Errors(6).Ignore = True 'Lock Error
        .Errors(7).Ignore = True 'Unprotected Forumla Error
        .Errors(8).Ignore = True 'Data Validation Error
        .Errors(9).Ignore = True 'Inconsistent Error
        .Errors(10).Ignore = True 'Inconsistent Calculated Column Formula Error
      End With
     
    Next cel
  End With
  Application.ScreenUpdating = True
End Sub


Whoops. Sorry:

VBA Code:
Option Explicit
Private Sub Workbook_Open()

    If ActiveSheet.Name = Sheet1.Name Then
    'Start Timeout Timer
        checktime = False
        Call checktimer
    'Check Timeout timer
        checktime = True
        Lastchange = Now()
     End If
   
   
  Dim cel As Range
 
  Application.ScreenUpdating = False
  ThisWorkbook.RefreshAll
  With Sheets("IPS Cases")
    On Error Resume Next
    ThisWorkbook.Sheets("IPS Cases").Range("B2").Sort Key1:=ThisWorkbook.Sheets("IPS Cases").Range("B3"), _
Order1:=xlAscending, Header:=xlYes, _
Key2:=ThisWorkbook.Sheets("IPS Cases").Range("F3"), Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
    On Error GoTo 0
    
    For Each cel In .Range("A2:AR200") 'Ignore ALL Errors
      With cel
        .Errors(6).Ignore = True 'Lock Error
        .Errors(7).Ignore = True 'Unprotected Forumla Error
        .Errors(8).Ignore = True 'Data Validation Error
        .Errors(9).Ignore = True 'Inconsistent Error
        .Errors(10).Ignore = True 'Inconsistent Calculated Column Formula Error
      End With
      
    Next cel
  End With
  Application.ScreenUpdating = True
End Sub

Changed B to F:
Key2:=ThisWorkbook.Sheets("IPS Cases").Range("F3"), Order2:=xlAscending, Header:=xlYes, _
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,157
Members
452,615
Latest member
bogeys2birdies

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