macro sorting sheets in ascending order

tuytuy

Board Regular
Joined
Mar 28, 2013
Messages
75
i was tyring to write a code to sort the sheets in ascending order.
this is what i got but all it does is put all the sheets with which name starts by a 1 in front then put the other one in ascending order.

EG.

1, 10, 11, 2, 21, 22, 3, 4, 5, 6, 7, 8, 9

Code:
Dim i As Integer, j As Integer, h As Integerh = Sheets.CountOn Error GoTo ErrorTrap:For i = 1 To h - 1    For j = i + 1 To h        If Sheets(j).Name < Sheets(i).Name Then        Sheets(j).Move Before:=Sheets(i)        End If    Next    NextErrorTrap:
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If all the sheets have numeric names then maybe try something like this...
If CInt(Sheets(j).Name) < CInt(Sheets(i).Name) Then
 
Upvote 0
Code:
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Integer[/color], j [color=darkblue]As[/color] [color=darkblue]Integer[/color], h [color=darkblue]As[/color] [color=darkblue]Integer[/color]
    h = Sheets.Count
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] ErrorTrap
    [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] h - 1
        [color=darkblue]For[/color] j = i + 1 [color=darkblue]To[/color] h
            [color=darkblue]If[/color] Format(Sheets(j).Name, "000") < Format(Sheets(i).Name, "000") [color=darkblue]Then[/color]
                Sheets(j).Move Before:=Sheets(i)
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]Next[/color]
    Next
ErrorTrap:
 
Upvote 0
Code:
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR], j [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR], h [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
    h = Sheets.Count
    [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] ErrorTrap
    [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] h - 1
        [COLOR=darkblue]For[/COLOR] j = i + 1 [COLOR=darkblue]To[/COLOR] h
            [COLOR=darkblue]If[/COLOR] Format(Sheets(j).Name, "000") < Format(Sheets(i).Name, "000") [COLOR=darkblue]Then[/COLOR]
                Sheets(j).Move Before:=Sheets(i)
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]Next[/COLOR]
    Next
ErrorTrap:

works fine thank you so much
 
Upvote 0

Forum statistics

Threads
1,214,619
Messages
6,120,550
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top