So I'm importing a CSV, formatting the date column and saving as an XLS file.
I'd like to be able to sort the date column. I have the following code and am stumped on how to sort the column. The date column is column 4 and has "date" as the header in the 1st row
$xl = new-object -comobject excel.application
$xl.visible = $false
$Workbook = $xl.workbooks.open(“$destination")
$xl.columns.autofit() >$null
$xl.Columns.Item('D').NumberFormat = "MM/dd/yyy"
$Workbook.SaveAs($final,1)
$Workbook.Saved = $True
$xl.Quit()
I've recorded the following macro yet cannot decipher how to change it to powershell. Any help is appreciated
Sub Macro1()
'
' Macro1 Macro
'
'
Columns("D:D").Select
ActiveWorkbook.Worksheets("newreport").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("newreport").Sort.SortFields.Add Key:=Range("D1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("newreport").Sort
.SetRange Range("A1:F251")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Sort-Objectwhen you import the CSV? e.g.Import-Csv x.csv | Sort-Object -Property { [datetime]$_.Date } | ....Header = xlYesif you want to mark the first row as header.