View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
JStiehl JStiehl is offline
external usenet poster
 
Posts: 35
Default Sort tabs numerically

Thanks Jacob. I tried both of the codes, but neither one sorted the tabs
with numbers in the proper order.

I tried the following code that I found, and it will sort the tabs with
numbers in the correct order, but put them at the end of all my tabs. I
changed the symbol and it will put the tabs with numbers at the beginning,
but will sort them in descending order. I am not very skilled at VBA--is
there anything you can suggest to tweak this code to make it sort tabs with
numbers in ascending order and put them at the beginning of the tabs?

Thanks so much, I really appreciate your help.

Sub WorksheetsSortAscending()
Dim sCount As Integer, i As Integer, j As Integer
sCount = Worksheets.Count
If sCount = 1 Then Exit Sub
For i = 1 To sCount - 1
For j = i + 1 To sCount
If Val(Worksheets(j).Name) Val(Worksheets(i).Name) Then
Worksheets(j).Move befo=Worksheets(i)
End If
Next j
Next i
End Sub

"Jacob Skaria" wrote:

Try this instead..

Sub Macro()
Dim lngCount1 As Long, lngCount2 As Long
For lngCount1 = 1 To Sheets.Count
For lngCount2 = lngCount1 + 1 To Sheets.Count
If Sheets(lngCount1).Name Sheets(lngCount2).Name Then _
Sheets(lngCount1).Move After:=Sheets(lngCount2): Exit For
Next
Next
End Sub


--
Jacob (MVP - Excel)


"Jacob Skaria" wrote:

Try

Sub Macro()
Dim lngCount1 As Long, lngCount2 As Long
For lngCount1 = 1 To Sheets.Count - 1
For lngCount2 = lngCount1 + 1 To Sheets.Count
If IsNumeric(Sheets(lngCount1).Name) And _
IsNumeric(Sheets(lngCount2).Name) Then
If CCur(Sheets(lngCount1).Name) CCur(Sheets(lngCount2).Name) Then _
Sheets(lngCount1).Move After:=Sheets(lngCount2): Exit For
End If
Next
Next
End Sub


--
Jacob (MVP - Excel)


"JStiehl" wrote:

I need help with VBA code to sort tab names with numbers in order, but leave
the ones with words as they are. Tab names with numbers should precede the
tabs with words. I have searched and can only find codes to sort
alphanumerically. Thanks for your help.

Example of what order I would like my tabs in:

123890
456678
789123
Project1
Project2
Project3
Total
Findings