PDA

View Full Version : [SOLVED] Sort Sheets in ascending order



PSL
05-11-2009, 09:08 AM
Hi,

I am trying to sort sheets in a workbook in a numerical order.

Suppose I have 10 sheets namely ; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Now when I try to sort them using VBA the result is :-

1, 10, 2, 3, 4, 5, 6, 7, 8

??

Here's the code i'm using:


Sub SortWorksheets()
Dim sCount As Integer, i As Integer, j As Integer
Application.ScreenUpdating = False
sCount = Worksheets.Count
If sCount = 1 Then Exit Sub
For i = 1 To sCount - 1
For j = i + 1 To sCount
If Worksheets(j).Name < Worksheets(i).Name Then
Worksheets(j).Move Before:=Worksheets(i)
End If
Next j
Next i
End Sub


Any solutions??

Tried the code from the KB as well.. But getting the same problem..

Thanks

georgiboy
05-11-2009, 09:35 AM
Try this...


Sub SortWorksheets()
Dim sCount As Integer, i As Integer, j As Integer
Application.ScreenUpdating = False
sCount = Worksheets.Count
If sCount = 1 Then Exit Sub
For i = 1 To sCount - 1
For j = i + 1 To sCount
If Int(Worksheets(j).Name) < Int(Worksheets(i).Name) Then
Worksheets(j).Move Before:=Worksheets(i)
End If
Next j
Next i
Application.ScreenUpdating = True
End Sub


Hope this helps

PSL
05-11-2009, 09:40 AM
If Int(Worksheets(j).Name) < Int(Worksheets(i).Name) Then

Perfect!!

Thanks a ton..

Regards