WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Excel
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Sorting Worksheets inside a Workbook


:OpenExcelFileSortWorksheets

SortAsc = @TRUE      ; how to sort

xlsFilename = AskLine("Excel OLE Example", "Enter the Path & Filename", "C:\test\MyExcelFile.xls")

Excel = ObjectOpen("Excel.Application")
Excel.visible = @TRUE
Excel.Workbooks.Open(xlsFilename)
;
;message("debug", strcat("There are ", Excel.Workbooks(1).Worksheets.count, " worksheets in this workbook."))

wrkBook = Excel.Workbooks(1)
wsList = ""

;   first inventory the workbook...
For x = 1 To wrkBook.Worksheets.count
   wsList = ItemInsert(wrkBook.Worksheets(x).name, -1, wsList, @LF)  ; use LF just for nice display...
Next
;   sort the list...
wsList = ItemSort(wsList, @LF)
;message("Debug", wsList)

;   from the macro...
;    Sheets("Southwest").Select
;    Sheets("Southwest").Move Before:=Sheets(1)

If SortAsc
   ;   move in reverse order to sort Ascending...
   For x = ItemCount(wsList, @LF) To 1 By -1
      ThisName = ItemExtract(x, wsList, @LF)
      ;   grab named worksheet, make it the first sheet...
      wrkBook.Worksheets(ThisName).move(::Before=wrkbook.Worksheets(1))
   Next
Else
   ;   ...other direction to sort Descending...
   For x = 1 To ItemCount(wsList, @LF)
      ThisName = ItemExtract(x, wsList, @LF)
      ;   grab named worksheet, make it the first sheet...
      wrkBook.Worksheets(ThisName).move(::Before=wrkbook.Worksheets(1))
   Next
EndIf

Excel.Workbooks(1).Save()
Excel.quit
Excel = 0
Return

Article ID:   W17163
File Created: 2007:07:03:14:28:34
Last Updated: 2007:07:03:14:28:34