Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default how to sort worksheets

I have tried Chip Pearson's sorting codes and I can't get it to work on my
worksheet names, maybe it can't be done... My worksheet names are created by
code...the names are as follows:
01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2); 12-01-05 CSB 18" RCP
after the sort I would like the order to be:
12-01-05 CSB 18" RCP; 01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2)...
Can this be done?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default how to sort worksheets

That sort magic is all in the comparison of the names to each other.

You will need to modify Pearson's code to do a different type of comparison

Extract the first 8 characters from the name and convert that to a date.
then compare the dates

if they match, then compare the remainder.

--
Regards,
Tom Ogilvy


"jnf40" wrote in message
...
I have tried Chip Pearson's sorting codes and I can't get it to work on my
worksheet names, maybe it can't be done... My worksheet names are created
by
code...the names are as follows:
01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2); 12-01-05 CSB 18" RCP
after the sort I would like the order to be:
12-01-05 CSB 18" RCP; 01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2)...
Can this be done?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default how to sort worksheets

Another option would be to rename your existing worksheets using a format like:

yyyy-mm-dd XXX ###" XXX

You may want to make sure that the inches portion includes enough characters for
the worse case scenario:

So for example:
2006-01-28 CSB 001" RCP



jnf40 wrote:

I have tried Chip Pearson's sorting codes and I can't get it to work on my
worksheet names, maybe it can't be done... My worksheet names are created by
code...the names are as follows:
01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2); 12-01-05 CSB 18" RCP
after the sort I would like the order to be:
12-01-05 CSB 18" RCP; 01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2)...
Can this be done?


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default how to sort worksheets

Thanks for the response...you make it sound so easy, but I'm not that good
with vba to know where to begin with that.

"Tom Ogilvy" wrote:

That sort magic is all in the comparison of the names to each other.

You will need to modify Pearson's code to do a different type of comparison

Extract the first 8 characters from the name and convert that to a date.
then compare the dates

if they match, then compare the remainder.

--
Regards,
Tom Ogilvy


"jnf40" wrote in message
...
I have tried Chip Pearson's sorting codes and I can't get it to work on my
worksheet names, maybe it can't be done... My worksheet names are created
by
code...the names are as follows:
01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2); 12-01-05 CSB 18" RCP
after the sort I would like the order to be:
12-01-05 CSB 18" RCP; 01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2)...
Can this be done?




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default how to sort worksheets

This modification of Chip's code worked for me with your samples.

Sub SortWorksheets()

Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
Dim bGreater As Boolean

SortDescending = False

If ActiveWindow.SelectedSheets.Count = 1 Then
FirstWSToSort = 1
LastWSToSort = Worksheets.Count
Else
With ActiveWindow.SelectedSheets
For N = 2 To .Count
If .Item(N - 1).Index < .Item(N).Index - 1 Then
MsgBox "You cannot sort non-adjacent sheets"
Exit Sub
End If
Next N
FirstWSToSort = .Item(1).Index
LastWSToSort = .Item(.Count).Index
End With
End If

For M = FirstWSToSort To LastWSToSort
For N = M To LastWSToSort
sN = Worksheets(N).Name
sM = Worksheets(M).Name
dtN = CDate(Left(sN, 8))
dtM = CDate(Left(sM, 8))
sN1 = Right(sN, Len(sN) - 8)
sM1 = Right(sM, Len(sM) - 8)
If dtN dtM Then
bGreater = True
ElseIf dtN < dtM Then
bGreater = False
Else
If StrComp(sN1, sM1, vbTextCompare) = 0 Then
bGreater = True
Else
bGreater = False
End If
End If
If SortDescending = True Then
If bGreater Then
Worksheets(N).Move Befo=Worksheets(M)
End If
Else
If Not bGreater Then
Worksheets(N).Move Befo=Worksheets(M)
End If
End If
Next N
Next M

End Sub



--
Regards,
Tom Ogilvy

"jnf40" wrote in message
...
Thanks for the response...you make it sound so easy, but I'm not that good
with vba to know where to begin with that.

"Tom Ogilvy" wrote:

That sort magic is all in the comparison of the names to each other.

You will need to modify Pearson's code to do a different type of
comparison

Extract the first 8 characters from the name and convert that to a date.
then compare the dates

if they match, then compare the remainder.

--
Regards,
Tom Ogilvy


"jnf40" wrote in message
...
I have tried Chip Pearson's sorting codes and I can't get it to work on
my
worksheet names, maybe it can't be done... My worksheet names are
created
by
code...the names are as follows:
01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2); 12-01-05 CSB 18" RCP
after the sort I would like the order to be:
12-01-05 CSB 18" RCP; 01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2)...
Can this be done?








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default how to sort worksheets

Thank you both...Tom's worked fine...thanks again

"Dave Peterson" wrote:

Another option would be to rename your existing worksheets using a format like:

yyyy-mm-dd XXX ###" XXX

You may want to make sure that the inches portion includes enough characters for
the worse case scenario:

So for example:
2006-01-28 CSB 001" RCP



jnf40 wrote:

I have tried Chip Pearson's sorting codes and I can't get it to work on my
worksheet names, maybe it can't be done... My worksheet names are created by
code...the names are as follows:
01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2); 12-01-05 CSB 18" RCP
after the sort I would like the order to be:
12-01-05 CSB 18" RCP; 01-01-06 CSB 18" RCP; 01-01-06 CSB 18" RCP (2)...
Can this be done?


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort Worksheets (without VB) Elgee Excel Worksheet Functions 4 August 16th 08 03:03 PM
Sort worksheets donbowyer Excel Programming 2 September 22nd 06 01:06 PM
Sort Worksheets mate Excel Worksheet Functions 2 March 24th 05 11:05 AM
Sort Numerically Worksheets via VB when creating a new worksheets John Excel Programming 6 June 1st 04 07:21 AM
HELP sort 2 worksheets Malycom Excel Programming 2 February 16th 04 09:55 AM


All times are GMT +1. The time now is 03:56 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"