+ Reply to Thread
Results 1 to 7 of 7

Excel 2007 : Sort Sheet alphabetically.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Sort Sheet alphabetically.

    I need to know how to sort my tabs or sheets. the thing is i have a code that sorts ALL of my sheets alphabetically, but i need this macro to leave the others as is, and just sort the new sheet.


    This is my create sheet macro, it works great, but it throws it at the end of my workbook. i need it to put it alphabetically with the others.

    Sub createsheet()
    Dim SearchData As String
    Application.ScreenUpdating = False
    
    SearchData = InputBox("Enter the tag number.")
    
    Sheets("NEW").Copy Before:=Sheets("NEW")
    Sheets("NEW (2)").Activate
    ActiveSheet.Name = SearchData
    Sheets("INVENTORY").Activate
        Range("B5").Select
        Selection.AutoFill Destination:=Range("B5:B731"), Type:=xlFillDefault
        Range("B5:B731").Select
    Sheets("autoship").Activate
    End Sub
    this is my sort sheets macro, but i dont want to do this every time i create one single sheet as it takes about 10 minutes for this macro to finish.

    Sub SortSheets()
    Dim i, j As Integer
    Dim iNumSheets As Integer
    
    iNumSheets = ActiveWorkbook.Sheets.Count
    
    Application.ScreenUpdating = False
    For i = 1 To iNumSheets - 1
    For j = i + 1 To iNumSheets
    If UCase(Sheets(i).Name) > UCase(Sheets(j).Name) Then
    Sheets(j).Move Before:=Sheets(i)
    End If
    Next j
    Next i
    Application.ScreenUpdating = True
    End Sub

    thanks in advance

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Sort Sheet alphabetically.

    Maybe this:
    Option Explicit
    
    Sub CreateSheet()
    Dim SearchData As String
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    
    SearchData = InputBox("Enter the tag number.")
    
    Sheets("NEW").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = SearchData
    Sheets("INVENTORY").Activate
        Range("B5").AutoFill Destination:=Range("B5:B731"), Type:=xlFillDefault
        Range("B5:B731").Select
    Sheets("autoship").Activate
    
    'move sheet alphabetically
        For Each ws In Worksheets
            If ws.Name <> SearchData And Sheets(ws.Index - 1).Name < SearchData Then _
                    Sheets(SearchData).Move Before:=ws
        Next ws
        
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Sort Sheet alphabetically.

    I'm getting a subscript out of range error. =\

  4. #4
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470

    Re: Sort Sheet alphabetically.

    Quote Originally Posted by GaidenFocus View Post
    I'm getting a subscript out of range error. =\
    the range may be merged cell , plz check it again

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Sort Sheet alphabetically.

    Since it works for me, I'd have to see your workbook to understand the problem.

  6. #6
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Sort Sheet alphabetically.

    yea i can't post the workbook, it contains alot of sensitive information.

    this is the line its giving me the error with.

    If ws.Name <> SearchData And Sheets(ws.Index - 1).Name < SearchData Then Sheets(SearchData).Move Before:=ws

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Sort Sheet alphabetically.

    Since all we're working on now is sorting by sheet name, post your workbook without the data in it. We can test just that part on your actual sheet names.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1