A vendor I work with regularly supplies me with .CSV files with problematic formatting. I am writing a Macro to deal with them and format them and sort each time I receive one.
The Macro below asks for the file location, then imports the CSV, automatically formatting column C as Dates. It asks the User for a Start and End date to define a range (this is to be used in the near future but not yet), and then is supposed to sort the sheet, rows A-H, by date (column C) in descending order.
Only problem is, the macro does not cause the sheet to sort.
Can anyone tell me why the operation fails?
000Reports.ods (15.2 KB)
Sample Sheet attached, for testing purposes and to show the formatting of the CSV supplied by my vendor. It does not contain the macro code listed below. NOTE: Even though this file has an ODS extension, it is in fact a CSV file because otherwise this website will not allow it to be uploaded.
Sorry for the long macro code; I know itâs often important to see the entire thing in order to debug a single line.
Global REPORT1 As String
Sub OpenCSVFile
Dim FileDialog As Object
Dim oDoc As Object
Dim oSheet As Object
Dim oCellRange As Object
Dim oSortDesc(0) As New com.sun.star.beans.PropertyValue
Dim oSortDescOrder As New com.sun.star.beans.PropertyValue
' Create a file dialog
FileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
' Set the dialog title
FileDialog.setTitle("Select your ActiveReports.csv file")
' Set the dialog to open .csv files
FileDialog.appendFilter("CSV Files", "*.csv")
' Display the dialog and get the selected file path
If FileDialog.execute() Then
REPORT1 = FileDialog.Files(0)
Else
MsgBox "No file selected.", 64, "Error"
Exit Sub
End If
' Define properties for opening the CSV file
Dim FileProps(2) As New com.sun.star.beans.PropertyValue
FileProps(0).Name = "FilterName"
FileProps(0).Value = "Text - txt - csv (StarCalc)"
FileProps(1).Name = "FilterOptions"
FileProps(1).Value = "44,34,76,6,1/1/2/1/3/3,,true,true,true" ' The third field (Column C) is defined as a date field with the format MM-DD-YY
FileProps(2).Name = "Hidden"
FileProps(2).Value = False
' Open the selected file
oDoc = StarDesktop.loadComponentFromURL(ConvertToURL(REPORT1), "_blank", 0, FileProps())
oSheet = oDoc.Sheets(0)
' Ask the user for a START DATE
Dim StartDate As String
StartDate = InputBox("Enter the START DATE in the format MM/DD/YY:", "Input START DATE")
' Validate the START DATE
If Not IsDate(StartDate) Then
MsgBox "Invalid START DATE. Please enter a valid date in the format MM/DD/YY.", 48, "Error"
Exit Sub
End If
' Ask the user for an END DATE
Dim EndDate As String
EndDate = InputBox("Enter the END DATE in the format MM/DD/YY:", "Input END DATE")
' Validate the END DATE
If Not IsDate(EndDate) Then
MsgBox "Invalid END DATE. Please enter a valid date in the format MM/DD/YY.", 48, "Error"
Exit Sub
End If
' Convert the dates to Date data type
Dim StartDateAsDate As Date
Dim EndDateAsDate As Date
StartDateAsDate = CDate(StartDate)
EndDateAsDate = CDate(EndDate)
' Check if the END DATE is later than the START DATE
If EndDateAsDate < StartDateAsDate Then
MsgBox "END DATE must be later than START DATE.", 48, "Error"
Exit Sub
End If
' Define the range to sort (excluding headers)
oCellRange = oSheet.getCellRangeByName("A2:H54")
' Define the sort field (column C, descending)
oSortDesc(0).Name = "SortKey"
oSortDesc(0).Value = 2
' Define the sort order
oSortDescOrder.Name = "IsSortAscending"
oSortDescOrder.Value = False
' Sort the data
oCellRange.sort(Array(oSortDesc(0), oSortDescOrder))
End Sub