How to sort multiple columns in Excel thru Access

Pearlple

New Member
Joined
May 28, 2019
Messages
2
Hello! I'm a newbie here.
I would like to ask for help since I've been searching elsewhere and I cannot find an answer.
I'm running VBA at Excel thru Access since I download the data from Access itself.
I have already added in Access a query to sort the columns before the exporting to Excel.
However, as a precaution, I want to sort it again at Excel.
My code below to sort the columns is working but only up to 3 columns.
When I add key4 and 5 and order4 and 5, respectively, the whole things stops.
Can anyone help me to fix this?
Dim FilePath As String
Dim SheetName As String
Dim xlApp As Object
Dim xlWorkbook As Object
Dim xlSheet As Object

Set xlApp = CreateObject("Excel.Application")

FilePath = "XXXX ".xls"
SheetName = "YYYY"

Set xlWorkbook = xlApp.Workbooks.Open(FilePath)


Set xlSheet = xlWorkbook.Worksheets(SheetName)
xlApp.Visible = True
xlApp.DisplayAlerts = False
xlSheet.Select

With xlSheet
Dim LastRow As Long
LastRow = .UsedRange.Rows.Count

If LastRow > 2 Then
.Range("A2:J" & LastRow).Sort _
Key1:=xlSheet.Range("C2"), _ ‘TEXT IN ASCENDING ORDER
Key2:=xlSheet.Range("B2"), _ ‘TEXT IN ASCENDING ORDER
Key3:=xlSheet.Range("D2"), _ ‘TEXT IN ASCENDING ORDER
Key4:=xlSheet.Range("J2"), _ ‘DATE IN DESCENDING ORDER
Key5:=xlSheet.Range("I2"), _ ‘NUMBER IN DESCENDING ORDER
Order1:=1, Order2:=1, Order3:=1, Order4:=2, Order5:=2
Else
End If


Set xlApp = Nothing
Set xlSheet = Nothing
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,213,527
Messages
6,114,142
Members
448,551
Latest member
Sienna de Souza

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