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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,473
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

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,473
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

tuytuy

Board Regular
Joined
Mar 28, 2013
Messages
75
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,195,858
Messages
6,011,978
Members
441,661
Latest member
Pammie007

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
Top