Custom Sorting in VBA

eli_m

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

I have the code below but I can't get this part work:

VBA Code:
        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, _
        Key3:=ThisWorkbook.Sheets("IPS Cases").Range("H3"), Order2:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom

Basically, I want to sort by Column B first, then Column F then Column H.
B = Date
F = Name
H = Work Site

I always want to have it sorted by date but if the dates are the same I want it to sort by their name AFTER date then if the names are the same then Work Site but only after B and F.

Full 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, _
        Key2:=ThisWorkbook.Sheets("IPS Cases").Range("F3"), Order2:=xlAscending, Header:=xlYes, _
        Key3:=ThisWorkbook.Sheets("IPS Cases").Range("H3"), 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

Thanks in advance
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Shouldn't you be using Order3 with Key3? I have not done this in vba but I got that notion from here
Good pick up! This made me a look a little harder and find that I actually needed to do H before F for it to work for my needs. I also changed the order to 3.
 
Upvote 0
Nice to know I can still help when I have no experience with something!
Thanks for the recognition.
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,161
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