+ Reply to Thread
Results 1 to 4 of 4

How to Short sheet name by two kinds of sheets names( alpabet then numeric )

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-11-2009
    Location
    Indonesia
    MS-Off Ver
    Excel 2019
    Posts
    252

    How to Short sheet name by two kinds of sheets names( alpabet then numeric )

    Hi,
    How to sort the sheet name from the alphabet and then numbers
    Sub Sort_Active_Book()
    Dim i As Integer
    Dim j As Integer
    Dim iAnswer As VbMsgBoxResult
    iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
    & "Clicking No will sort in Descending Order", _
    vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
    For i = 1 To Sheets.Count
    For j = 1 To Sheets.Count - 1
    If iAnswer = vbYes Then
    If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
    Sheets(j).Move After:=Sheets(j + 1)
    End If
    ElseIf iAnswer = vbNo Then
    If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
    Sheets(j).Move After:=Sheets(j + 1)
    End If
    End If
    Next j
    Next i
    End Sub
    Sheet names : 1,2,3,a,b,c
    above code produces the sequence: ---> 1,2,3,a,b,c
    Want to be sorted into--> a,b,c,1,2,3

    Thanks in advance
    Last edited by herukuncahyono; 09-19-2013 at 12:17 AM.

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to Short sheet name by two kinds of sheets names( alpabet then numeric )

    Maybe do something like:
    Option Explicit
    
    Sub Sort_Active_Book()
    Dim i As Integer
    Dim j As Integer
    Dim iAnswer As VbMsgBoxResult
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Sheets
        On Error GoTo change_name
        ws.Name = Int(ws.Name)
        On Error GoTo 0
    Next
    iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
    & "Clicking No will sort in Descending Order", _
    vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
    For i = 1 To Sheets.Count
    For j = 1 To Sheets.Count - 1
    If iAnswer = vbYes Then
    If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
    Sheets(j).Move After:=Sheets(j + 1)
    End If
    ElseIf iAnswer = vbNo Then
    If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
    Sheets(j).Move After:=Sheets(j + 1)
    End If
    End If
    Next j
    Next i
    For Each ws In ActiveWorkbook.Sheets
        If ws.Name < 1 Then
            ws.Name = Mid(Cells(1, ws.Name * 1000).Address, 2, 1)
        End If
    Next
    Exit Sub
    change_name:
    ws.Name = Cells(1, "" & ws.Name & "").Column / 1000
    Resume Next
    End Sub
    Might not work if your real data is very different to your example though.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,641

    Re: How to Short sheet name by two kinds of sheets names( alpabet then numeric )

    Try
    Sub test()
        Dim ws As Worksheet, AL As Object, x As Object, i As Long, res As VbMsgBoxResult
        res = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
        & "Clicking No will sort in Descending Order", _
        vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
        If res = vbCancel Then Exit Sub
        Set AL = CreateObject("System.Collections.ArrayList")
        Set x = AL.Clone
        For Each ws In Sheets
            If IsNumeric(ws.Name) Then
                x.Add Val(ws.Name)
            Else
                AL.Add ws.Name
            End If
        Next
        AL.Sort: x.Sort
        If res = vbNo Then AL.Reverse: x.Reverse
        If AL.Count > 0 Then
            If x.Count > 0 Then
                AL.AddRange x
            End If
        Else
            Set AL = x
        End If
        For i = AL.Count - 1 To 0 Step -1
            Sheets(CStr(AL(i))).Move before:=Sheets(1)
        Next
    End Sub

  4. #4
    Forum Contributor
    Join Date
    03-11-2009
    Location
    Indonesia
    MS-Off Ver
    Excel 2019
    Posts
    252

    Re: How to Short sheet name by two kinds of sheets names( alpabet then numeric )

    hi jindon,
    Thanks for your code..its work...nice

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 17
    Last Post: 02-01-2013, 12:20 PM
  2. I can't save long file names, only short one, how can I do this?
    By short file names in forum Excel General
    Replies: 0
    Last Post: 01-03-2006, 01:15 PM
  3. [SOLVED] reference to sheets without using sheet names
    By Bob Phillips in forum Excel Formulas & Functions
    Replies: 54
    Last Post: 09-06-2005, 08:05 PM
  4. reference to sheets without using sheet names
    By Wes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10:05 PM
  5. [SOLVED] reference to sheets without using sheet names
    By Wes in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 09-03-2005, 04:05 PM

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