I have this VBA code that sorts dates in I5:I. Sometimes, I also have dates in column…
- User
I have this VBA code that sorts dates in I5:I. Sometimes, I also have dates in column H5:H which fall to the bottom of the sort. Is it possible to also sort the dates in H5:H Here is my current code: Public Sub SortTasksByDate() Application.EnableEvents = False Application.ScreenUpdating = False Dim lastRow As Long lastRow = ActiveSheet.Range("J" & Rows.count).End(xlUp).Row With Range("B5:L" & lastRow) .Sort Key1:=ActiveSheet.Range("I5:I" & lastRow), Order1:=xlAscending, Header:=xlYes End With Application.ScreenUpdating = True Application.EnableEvents = True End Sub
- Assistant
Yes, it is possible to also sort the dates in column H (H5:H) along with the dates in column I (I5:I). You can modify your code as follows:
Public Sub SortTasksByDate() Application.EnableEvents = False Application.ScreenUpdating = False Dim lastRow As Long lastRow = ActiveSheet.Range("J" & Rows.Count).End(xlUp).Row With Range("B5:L" & lastRow) .Sort Key1:=ActiveSheet.Range("I5:I" & lastRow), Order1:=xlAscending, Header:=xlYes .Sort Key1:=ActiveSheet.Range("H5:H" & lastRow), Order1:=xlAscending, Header:=xlYes, MatchCase:=False, _ Orientation:=xlTopToBottom, DataOption1:=xlSortNormal End With Application.ScreenUpdating = True Application.EnableEvents = True End Sub
In the above code, an additional line has been added after the first sorting line to sort the dates in column H (H5:H) in ascending order.
Note that the MatchCase, Orientation, and DataOption1 arguments have been specified for the second sorting line. You can modify these arguments as per your specific requirements.
- User
I have this VBA code that sorts my data rows by dates in I5:I. Sometimes, I also have dates in column H5:H which are positioned at the bottom of the sort because there are no dates in their row of column I. I would also like to sort the dates in H5:H but I would also like the rows concerned to remain at the bottom of the initial sort. Here is my current code: Public Sub SortTasksByDate() Application.EnableEvents = False Application.ScreenUpdating = False Dim lastRow As Long lastRow = ActiveSheet.Range("J" & Rows.count).End(xlUp).Row With Range("B5:L" & lastRow) .Sort Key1:=ActiveSheet.Range("I5:I" & lastRow), Order1:=xlAscending, Header:=xlYes End With Application.ScreenUpdating = True Application.EnableEvents = True End Sub
- Assistant
To sort the dates in column H (H5:H) and keep the rows at the bottom of the initial sort, you can modify your code as follows:
Public Sub SortTasksByDate() Application.EnableEvents = False Application.ScreenUpdating = False Dim lastRow As Long lastRow = ActiveSheet.Range("J" & Rows.Count).End(xlUp).Row With Range("B5:L" & lastRow) .Sort Key1:=ActiveSheet.Range("I5:I" & lastRow), Order1:=xlAscending, Header:=xlYes .Sort Key1:=ActiveSheet.Range("H5:H" & lastRow), Order1:=xlAscending, Header:=xlYes, MatchCase:=False, _ Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, CustomOrder:=Join(getLastRows(), ",") End With Application.ScreenUpdating = True Application.EnableEvents = True End Sub
- User
I am getting an error 'Named argument not found' on the line 'CustomOrder:='
- Assistant
Apologies for the confusion. It seems that the CustomOrder argument is not available for sorting in VBA.
To achieve the desired result, you can modify the code to perform a two-step sorting process.
Here's the updated code:
Public Sub SortTasksByDate() Application.EnableEvents = False Application.ScreenUpdating = False Dim lastRow As Long lastRow = ActiveSheet.Range("J" & Rows.Count).End(xlUp).Row With Range("B5:L" & lastRow) ' Sort by column I (ascending) .Sort Key1:=ActiveSheet.Range("I5:I" & lastRow), Order1:=xlAscending, Header:=xlYes ' Find the last row with a date in column H Dim i As Long Dim lastHRow As Long For i = lastRow To 5 Step -1 If Not IsEmpty(Range("H" & i).Value) Then lastHRow = i Exit For End If Next i ' Sort by column H (ascending) from the first row with a date in column H to the last row .Sort Key1:=ActiveSheet.Range("H6:H" & lastHRow), Order1:=xlAscending, Header:=xlYes End With Application.ScreenUpdating = True Application.EnableEvents = True End Sub - User
It is still moving the row with dates in column H to the top of the original sort. I do not want the rows with dates in column H to be moved to the top of the original sort.
- Assistant
Apologies for the misunderstanding. To sort the dates in column H (H5:H) while keeping the rows with dates in column H at the bottom of the initial sort, you can use an array to store the original sort order of the data, perform the sort, and then rearrange the rows accordingly.
- User
When I applied your code, the row remained at the bottom of the initial sort but the values in column H moved to the top
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.