Saturday, April 3, 2010

Another Excel VBScript Sort Explained Beautifully

Another Excel VBScript Sort Explained Beautifully

What if we wanted to sort first by column C and then by column A. Admittedly, that won’t have much impact with this simple data set. But here’s what the script would look like:

Const xlAscending = 1
Const xlYes = 1

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = _
objExcel.Workbooks.Open("C:\Scripts\SortTest.xls")

Set objWorksheet = objWorkbook.Worksheets(1)
Set objRange = objWorksheet.UsedRange
Set objRange2 = objExcel.Range("C1")
Set objRange3 = objExcel.Range("A1")

objRange.Sort objRange2, xlAscending, objRange3, , xlAscending, , , xlYes

[Via]