I'm using python and pandas to query a table in SQL, store it in a DataFrame, then write it to an excel file (.xlsx).
I'm then using a couple of VBA macros to loop through the columns and do some conditional formatting to highlight outliers.
Everything works fine except the date column which excel gets stuck on and presents an error:
"Method 'Average' of object 'WorksheetFunction' failed"
The date is being stored as a string in the format '20-01-2022' which is presumably causing the error so I need to convert it to an actual datetime format that excel will recognise upon opening the file.
Example:
import pandas as pd
df = pd.DataFrame([[1, '21-06-2022'], [2, '19-08-2022'], [3, '06-04-2022']], columns=['id', 'date'])
df.to_excel("output.xlsx")
If you then open "output.xlsx" and try to use conditional formatting on the 'date' column, or try to =AVERAGE(C2:C4)
either nothing happens or you get an error. If you double click into the cell, something happens and excel will suddenly recognise it, but this solution isn't suitable with thousands of cells.
How can I convert dates to a format that excel will recognise immediately upon opening the file?