View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
PVANS PVANS is offline
external usenet poster
 
Posts: 68
Default Macro to Loop through and sort worksheets

Good morning

I have a workbook with about 30 worksheets that need to be sorted by two
columns, date and currency.

I recorded a macro to sort a single worksheet (after discovering you cannot
sort multiple worksheets at once), and then went online to try and find a
macro code to loop through worksheets. I managed to find the following:

Sub SortData()

' SortData Macro

Dim ws As Worksheet
Select Case ws.Name
Case "Percentages", "MasterNonDMA%", "MasterNonDMA", "MasterDMA%",
"MasterDMA", "Reciept Saxo", "Model Account"
Exit Sub 'do nothing in fact
Case Else
'THIS IS WHERE MY RECORDED CODE STARTS
Cells.Select
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("H2") _
, Order2:=xlAscending, Header:=xlYes, OrderCustom:=1,
MatchCase:=False _
, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2:= _
xlSortNormal
'your sorting code
'THIS IS WHERE MY RECORDED CODE FINISHES
End Select


End Sub

However, when I attempt to run the code, I recieve the error:
Runtime error '91':
Object variable or With Block variable not set.

If I debug the problem, it highlights the following line:
Select Case ws.Name

I am sure I have made a silly error, or overlooked something relatively
simple, but I am completely stuck. Could someone please assist me?

Thank you

Regards