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?
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
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