I have data in column B that starts from B18. All the data starts with a text date in…
- User
I have data in column B that starts from B18. All the data starts with a text date in the format dd/mm/yyyy I would like to sort the range by the text date format with the oldest dat at the top. Can you please write a VBA code to do this
- Assistant
Certainly! Here is a VBA code that will sort your data in column B based on the text date format with the oldest date at the top:
vbaSub SortByDate() Dim ws As Worksheet Dim lastRow As Long ' Set the worksheet where your data resides Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with your actual sheet name ' Find the last row with data in column B lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' Sort the range in column B by date, oldest to newest With ws.Sort .SortFields.Clear .SortFields.Add Key:=Range("B18:B" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SetRange Range("B18:B" & lastRow) .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.