Trying to do custom sort in VBA

phyxius117

New Member
Joined
May 14, 2024
Messages
6
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hey all,

After looking up guides, I tried to incorporate a line of code to mimic custom sort for my end product of this macro. Basically this macro takes existing tabs of active worksheet, copies them then perform some operations on them within the new worksheet. For some reason my sorting code isn't working. The Relevant Section is in blue and the sorting line is in red color. Can anyone tell me why and how to fix it? Thanks.

VBA Code:
Sub Export()
Set wbThis = ThisWorkbook

Dim DCname As String
    DCname = ActiveWorkbook.Sheets("Reference").Range("R2").Value

Dim FiscalYear As String
    FiscalYear = ActiveWorkbook.Sheets("Reference").Range("R8").Value

Dim Period As String
    Period = ActiveWorkbook.Sheets("Reference").Range("R11").Value

Dim FileName As String
    If Worksheets("Reference").Range("R8") = "Test" Then
    FileName = InputBox("Enter File Name")
    Else
    FileName = DCname & "_SQL Data_" & FiscalYear & "_" & Period
    End If
    
Dim Visible As XlSheetVisibility
    Worksheets("Route Totals").Visible = xlSheetVisible
    Worksheets("Stops").Visible = xlSheetVisible
    Worksheets("Domicile Rates").Visible = xlSheetVisible
    
Worksheets(Array("Route Totals", "Stops", "Domicile Rates")).Copy

    With ActiveWorkbook.Worksheets("Route Totals")
    .UsedRange.Value = .UsedRange.Value
    .Cells.Columns.AutoFit
    End With
    
[COLOR=rgb(41, 105, 176)]    With ActiveWorkbook.Worksheets("Stops")
    .UsedRange.Value = .UsedRange.Value
    Dim lastrow As Long
    lastrow = .Range("M" & Rows.Count).End(xlUp).Row
    .Range("L3").Formula = "=miles($AL$2,AL3)"
    .Range("L3:L" & lastrow).FillDown
    .Range("L3:L" & lastrow).Copy
    .Range("L3:L" & lastrow).PasteSpecial Paste:=xlPasteValues
    .Range("AM3").Formula = "=IF(G3=0,0,tolls(AL2,AL3,true)*-1.05)"
    .Range("AM3:AM" & lastrow).FillDown
    .Range("AM3:AM" & lastrow).Copy
    .Range("AM3:AM" & lastrow).PasteSpecial Paste:=xlPasteValues
    .Range("AN2").Formula = "=IF(G2=0,sumif(B:B,B2,AM:AM),0)"
    .Range("AN2:AN" & lastrow).FillDown
    .Range("AN2:AN" & lastrow).Copy
    .Range("AN2:AN" & lastrow).PasteSpecial Paste:=xlPasteValues
    .Range("AO2").Formula = "=IFERROR(VLOOKUP(K2,'Domicile Rates'!A:AP,42,FALSE),0)"
    .Range("AO2:AO" & lastrow).FillDown
    .Range("AO2:AO" & lastrow).Copy
    .Range("AO2:AO" & lastrow).PasteSpecial Paste:=xlPasteValues
    .Cells.Columns.AutoFit
  [/COLOR][COLOR=rgb(226, 80, 65)]  Columns.Sort Key1:=Columns("C"), Order1:=xlAscending, Key2:=Columns("B"), Order2:=xlAscending, Key3:=Columns("G"), Order3:=xlAscending, Header:=xlYes[/COLOR]
[COLOR=rgb(41, 105, 176)]    End With[/COLOR]
    
    With ActiveWorkbook.Worksheets("Domicile Rates")
    .Visible = Visible
    End With
    
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & FileName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

ThisWorkbook.Worksheets("Route Totals").Visible = Visible
ThisWorkbook.Worksheets("Stops").Visible = Visible
ThisWorkbook.Worksheets("Domicile Rates").Visible = Visible
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Sorry I'm kind of new to this forum. Not sure why the coloring of fonts didn't work.

Here is the relevant section of the code:
VBA Code:
    With ActiveWorkbook.Worksheets("Stops")
    .UsedRange.Value = .UsedRange.Value
    Dim lastrow As Long
    lastrow = .Range("M" & Rows.Count).End(xlUp).Row
    .Range("L3").Formula = "=miles($AL$2,AL3)"
    .Range("L3:L" & lastrow).FillDown
    .Range("L3:L" & lastrow).Copy
    .Range("L3:L" & lastrow).PasteSpecial Paste:=xlPasteValues
    .Range("AM3").Formula = "=IF(G3=0,0,tolls(AL2,AL3,true)*-1.05)"
    .Range("AM3:AM" & lastrow).FillDown
    .Range("AM3:AM" & lastrow).Copy
    .Range("AM3:AM" & lastrow).PasteSpecial Paste:=xlPasteValues
    .Range("AN2").Formula = "=IF(G2=0,sumif(B:B,B2,AM:AM),0)"
    .Range("AN2:AN" & lastrow).FillDown
    .Range("AN2:AN" & lastrow).Copy
    .Range("AN2:AN" & lastrow).PasteSpecial Paste:=xlPasteValues
    .Range("AO2").Formula = "=IFERROR(VLOOKUP(K2,'Domicile Rates'!A:AP,42,FALSE),0)"
    .Range("AO2:AO" & lastrow).FillDown
    .Range("AO2:AO" & lastrow).Copy
    .Range("AO2:AO" & lastrow).PasteSpecial Paste:=xlPasteValues
    .Cells.Columns.AutoFit
    Columns.Sort Key1:=Columns("C"), Order1:=xlAscending, Key2:=Columns("B"), Order2:=xlAscending, Key3:=Columns("G"), Order3:=xlAscending, Header:=xlYes
    End With
 
Upvote 0
@phyxius117 if you want to use custom formatting in your code when posting you need to use the Rich code tags option rather than VBA code tags ;)

Btw the first Columns in the line should be a range
Rich (BB code):
Columns.Sort Key1:=Columns("C"), Order1:=xlAscending, Key2:=Columns("B"), Order2:=xlAscending, Key3:=Columns("G"), Order3:=xlAscending, Header:=xlYes
 
Last edited:
Upvote 1
@phyxius117 if you want to use custom formatting in your code when posting you need to use the Rich code tags option rather than VBA code tags ;)

Btw the first Columns in the line should be a range
Rich (BB code):
Columns.Sort Key1:=Columns("C"), Order1:=xlAscending, Key2:=Columns("B"), Order2:=xlAscending, Key3:=Columns("G"), Order3:=xlAscending, Header:=xlYes
Hmm I modified and still it's not sorting...

VBA Code:
Columns("A:BK").Sort Key1:=Columns("C"), Order1:=xlAscending, Key2:=Columns("B"), Order2:=xlAscending, Key3:=Columns("G"), Order3:=xlAscending, Header:=xlYes
 
Upvote 0
Sorts fine for me, with
VBA Code:
Sub xxxx()
Range("A1:L21").Sort Key1:=Columns("C"), Order1:=xlAscending, Key2:=Columns("B"), Order2:=xlAscending, Key3:=Columns("G"), Order3:=xlAscending, Header:=xlYes
End Sub

Book1
ABCDEFGHIJKL
1Order IDOrder DateSiteSP NameCategory NameShip ViaQuantityOrder TotalCust SurnameBirthdateGender
2382187371690.058064
36536159761100.14206
419106610781980.190358
53449676471090.205516
6984632636490.215084
7746364361180.257336
834410310188750.263102
98446191039690.318292
10769136225440.334219
11834935986630.404677
129643610253280.438228
137946771056130.475686
144241049522990.489278
158810796832710.516712
166699252810710.614971
17349951494950.691229
184101632724710.744385
1959810102694110.75087
205943443961020.891264
2171485110598100.896692
Sheet1


becomes the below (see the highlighted lines)

Book1
ABCDEFGHIJKL
1Order IDOrder DateSiteSP NameCategory NameShip ViaQuantityOrder TotalCust SurnameBirthdateGender
24101632724710.744385
3382187371690.058064
46536159761100.14206
571485110598100.896692
64241049522990.489278
7834935986630.404677
834410310188750.263102
93449676471090.205516
108446191039690.318292
119643610253280.438228
12984632636490.215084
135943443961020.891264
147946771056130.475686
15746364361180.257336
1659810102694110.75087
17349951494950.691229
18769136225440.334219
196699252810710.614971
208810796832710.516712
2119106610781980.190358
Sheet1
 
Upvote 0
Are you on the actual sheet being sorted as the way you have it written it will only work if Worksheets("Stops") is the activesheet
 
Upvote 0
Maybe you just missed to add some dots:
Rich (BB code):
.Columns.Sort Key1:=.Columns("C"), Order1:=xlAscending, Key2:=.Columns("B"), Order2:=xlAscending, Key3:=.Columns("G"), Order3:=xlAscending, Header:=xlYes
 
Upvote 0
Maybe you just missed to add some dots:
Rich (BB code):
.Columns.Sort Key1:=.Columns("C"), Order1:=xlAscending, Key2:=.Columns("B"), Order2:=xlAscending, Key3:=.Columns("G"), Order3:=xlAscending, Header:=xlYes
Probably
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,961
Members
452,539
Latest member
delvey

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