My PS script is taking a .csv file and creating a excel .xlsx spreadsheet. I want to sort a particular column so the number come out in descending order.. I tried several changes to the sort code and still getting the error.
Unable to get the Sort property of the Range class
At H:\GoogleAnalytics\PowershellScripts\Sample1.ps1:79 char:1
[void] $objRange2.Sort($objRange)
This will open the .csv file from above and write it out as an Excel Spreadsheet
Write-Host “Begin converting the .csv report file to an Excel .XLSX Spreadsheet”
$csv = “H:/GoogleAnalytics/reports/”+$reportname+“webstats”+$datetime+“.csv” #Location of the source file
$xlsx = “H:/GoogleAnalytics/reports/”+$reportname+“webstats”+$datetime+“.xlsx” #Desired location of output
$delimiter = “,” #Specify the delimiter used in the file
Create a new Excel workbook with one empty sheet
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
$objRange = $worksheet.UsedRange
$objRange2 = $worksheet.Range(“C3”)
[void] $objRange2.Sort($objRange)
$worksheet.Name = $reportname
$worksheet.Cells.Item(1,1) = $TitleOfReport # get title of report from above variable
$result = $len - 8
$urltitle = $reportname.SubString(0,$result)
$urltitle = $urltitle+’ urls’
$worksheet.Cells.Item(1,2) = $urltitle.ToUpper()
$worksheet.Cells.Item(1,1).Font.Bold=$True # make the Report Title in cell A1 Bold
$worksheet.Cells.Item(1,2).Font.Bold=$True # make the Dept URL in cell B1 Bold
$worksheet.Cells.Item(2,1).Font.Bold=$True # make the Page Title in cell A2 Bold
$worksheet.Cells.Item(2,2).Font.Bold=$True # make the Page in cell B2 Bold
$worksheet.Cells.Item(2,3).Font.Bold=$True # make the Page Views in cell C2 Bold
$worksheet.Cells.Item(2,4).Font.Bold=$True # make the Avg Time on Pages in cell D2 Bold
$worksheet.Cells.Item(2,5).Font.Bold=$True # make the Users in cell E2 Bold
Build the QueryTables.Add command and reformat the data
$Workbook.SaveAs($xlsx,51)
Write-Host “Excel Spreadsheet Report found at: H:/GoogleAnalytics/reports/”$reportname’webstats’$datetime".XLSX"
$excel.Quit() #get-processexcel | stop-process -force
Write-Host “Deleting the .CSV Report file now that the Excel Spreadsheet has been created”
Remove-Item -path .\GoogleAnalytics\reports$reportname’webstats’$datetime.csv
Write-Host “Process Completed”
Please can you edit your post and use the </> button to format your code. This will make it much more readable and enable us to copy and paste for testing.
This will open the .csv file from above and write it out as an Excel Spreadsheet
Write-Host “Begin converting the .csv report file to an Excel .XLSX Spreadsheet”
$csv = “H:/GoogleAnalytics/reports/”+$reportname+“webstats”+$datetime+".csv" #Location of the source file
$xlsx = “H:/GoogleAnalytics/reports/”+$reportname+“webstats”+$datetime+".xlsx" #Desired location of output
$delimiter = “,” #Specify the delimiter used in the file
Create a new Excel workbook with one empty sheet
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
$objRange = $worksheet.UsedRange
$objRange2 = $worksheet.Range(“C3”)
[void] $objRange2.Sort($objRange)
$worksheet.Name = $reportname
$worksheet.Cells.Item(1,1) = $TitleOfReport # get title of report from above variable
$result = $len - 8
$urltitle = $reportname.SubString(0,$result)
$urltitle = $urltitle+’ urls’
$worksheet.Cells.Item(1,2) = $urltitle.ToUpper()
$worksheet.Cells.Item(1,1).Font.Bold=$True # make the Report Title in cell A1 Bold
$worksheet.Cells.Item(1,2).Font.Bold=$True # make the Dept URL in cell B1 Bold
$worksheet.Cells.Item(2,1).Font.Bold=$True # make the Page Title in cell A2 Bold
$worksheet.Cells.Item(2,2).Font.Bold=$True # make the Page in cell B2 Bold
$worksheet.Cells.Item(2,3).Font.Bold=$True # make the Page Views in cell C2 Bold
$worksheet.Cells.Item(2,4).Font.Bold=$True # make the Avg Time on Pages in cell D2 Bold
$worksheet.Cells.Item(2,5).Font.Bold=$True # make the Users in cell E2 Bold
Build the QueryTables.Add command and reformat the data
$TxtConnector = (“TEXT;” + $csv)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range(“A2”))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $delimiter
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1
Execute & delete the import query
$query.Refresh()
$query.Delete()
Save & close the Workbook as XLSX.
$Workbook.SaveAs($xlsx,51)
Write-Host “Excel Spreadsheet Report found at: H:/GoogleAnalytics/reports/”$reportname’webstats’$datetime".XLSX"
$excel.Quit()
#get-process excel | stop-process -force
Write-Host “Deleting the .CSV Report file now that the Excel Spreadsheet has been created”
Remove-Item -path .\GoogleAnalytics\reports$reportname’webstats’$datetime.csv
Write-Host “Process Completed”
Thanks Matt. I added the XISortOrder number 2 for descending… The column still comes out in the order of the .csv file. I created this simple 2 column .csv file. Column A has a team name, column B has a 3 digit number. There are only 7 rows… Do you have any idea why the sort is not working?
Are you adding the data to the sheet after you apply the sort? It appears that way from your script.
You should populate the data, then define the range where the data occurs, then sort.
Edit:
Incidently, the code I gave only sorts 1 column. If the value in column B is associated with the value in column A, you need to specify the range to be sorted, and the range you want to sort on: