Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default arranging worksheets in alphabetical order

I want to arrange the worksheets in a Excel worbook in alphabetical
order. I read the worksheet names into a list and sort it. When I try
to rearrange I am having problems. I get a run-time error "Move method
of worksheet class failed"

Any help will be appreciated.
Thanks,
Sunil

Code snippet:

long int numSheets;
CWorksheets Sheets;
CWorksheet wSheet;
Sheets = oBook.get_Worksheets( );
numSheets = Sheets.get_Count( );

for ( int i=1;i<=numSheets;i++ )
{
wSheet = Sheets.get_Item( COleVariant( (short)(i) ) );
wSheet.Activate( );
tmpName = wSheet.get_Name();
//AfxMessageBox( tmpName.c_str() );
if ( ( tmpName != "VE_Dump" ) && ( tmpName != "Summary"
) )
sheetNames.push_back( tmpName );
}

sheetNames.sort( );
//trial to see it it works
wSheet = Sheets.get_Item( COleVariant( (short)(1) ) );
wSheet.Move( COleVariant( (short)(3) ), covOptional );

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default arranging worksheets in alphabetical order

try chip pearson's code

Sub SortWorksheets()
'Chip Pearson

Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending 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
If SortDescending = True Then
If UCase(Worksheets(N).Name) _
UCase(Worksheets(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < _
UCase(Worksheets(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
End If
Next N
Next M

End Sub

--


Gary


wrote in message
oups.com...
I want to arrange the worksheets in a Excel worbook in alphabetical
order. I read the worksheet names into a list and sort it. When I try
to rearrange I am having problems. I get a run-time error "Move method
of worksheet class failed"

Any help will be appreciated.
Thanks,
Sunil

Code snippet:

long int numSheets;
CWorksheets Sheets;
CWorksheet wSheet;
Sheets = oBook.get_Worksheets( );
numSheets = Sheets.get_Count( );

for ( int i=1;i<=numSheets;i++ )
{
wSheet = Sheets.get_Item( COleVariant( (short)(i) ) );
wSheet.Activate( );
tmpName = wSheet.get_Name();
//AfxMessageBox( tmpName.c_str() );
if ( ( tmpName != "VE_Dump" ) && ( tmpName != "Summary"
) )
sheetNames.push_back( tmpName );
}

sheetNames.sort( );
//trial to see it it works
wSheet = Sheets.get_Item( COleVariant( (short)(1) ) );
wSheet.Move( COleVariant( (short)(3) ), covOptional );



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default arranging worksheets in alphabetical order

Thanks for the Info Gary. But I need to use C++ and not VBA, since I am
running this via an MFC app.

Gary Keramidas wrote:
try chip pearson's code

Sub SortWorksheets()
'Chip Pearson

Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending 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
If SortDescending = True Then
If UCase(Worksheets(N).Name) _
UCase(Worksheets(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < _
UCase(Worksheets(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
End If
Next N
Next M

End Sub

--


Gary


wrote in message
oups.com...
I want to arrange the worksheets in a Excel worbook in alphabetical
order. I read the worksheet names into a list and sort it. When I try
to rearrange I am having problems. I get a run-time error "Move method
of worksheet class failed"

Any help will be appreciated.
Thanks,
Sunil

Code snippet:

long int numSheets;
CWorksheets Sheets;
CWorksheet wSheet;
Sheets = oBook.get_Worksheets( );
numSheets = Sheets.get_Count( );

for ( int i=1;i<=numSheets;i++ )
{
wSheet = Sheets.get_Item( COleVariant( (short)(i) ) );
wSheet.Activate( );
tmpName = wSheet.get_Name();
//AfxMessageBox( tmpName.c_str() );
if ( ( tmpName != "VE_Dump" ) && ( tmpName != "Summary"
) )
sheetNames.push_back( tmpName );
}

sheetNames.sort( );
//trial to see it it works
wSheet = Sheets.get_Item( COleVariant( (short)(1) ) );
wSheet.Move( COleVariant( (short)(3) ), covOptional );


  #4   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default arranging worksheets in alphabetical order

Perhaps Chip's macro can help you:
http://www.cpearson.com/excel/sortws.htm

" wrote:

I want to arrange the worksheets in a Excel worbook in alphabetical
order. I read the worksheet names into a list and sort it. When I try
to rearrange I am having problems. I get a run-time error "Move method
of worksheet class failed"

Any help will be appreciated.
Thanks,
Sunil

Code snippet:

long int numSheets;
CWorksheets Sheets;
CWorksheet wSheet;
Sheets = oBook.get_Worksheets( );
numSheets = Sheets.get_Count( );

for ( int i=1;i<=numSheets;i++ )
{
wSheet = Sheets.get_Item( COleVariant( (short)(i) ) );
wSheet.Activate( );
tmpName = wSheet.get_Name();
//AfxMessageBox( tmpName.c_str() );
if ( ( tmpName != "VE_Dump" ) && ( tmpName != "Summary"
) )
sheetNames.push_back( tmpName );
}

sheetNames.sort( );
//trial to see it it works
wSheet = Sheets.get_Item( COleVariant( (short)(1) ) );
wSheet.Move( COleVariant( (short)(3) ), covOptional );


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default arranging worksheets in alphabetical order

You should be able to translate it easy enough.
Or ask in one the Excel Interop/SDK news groups.

NickHK

wrote in message
oups.com...
Thanks for the Info Gary. But I need to use C++ and not VBA, since I am
running this via an MFC app.

Gary Keramidas wrote:
try chip pearson's code

Sub SortWorksheets()
'Chip Pearson

Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending 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
If SortDescending = True Then
If UCase(Worksheets(N).Name) _
UCase(Worksheets(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < _
UCase(Worksheets(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
End If
Next N
Next M

End Sub

--


Gary


wrote in message
oups.com...
I want to arrange the worksheets in a Excel worbook in alphabetical
order. I read the worksheet names into a list and sort it. When I try
to rearrange I am having problems. I get a run-time error "Move method
of worksheet class failed"

Any help will be appreciated.
Thanks,
Sunil

Code snippet:

long int numSheets;
CWorksheets Sheets;
CWorksheet wSheet;
Sheets = oBook.get_Worksheets( );
numSheets = Sheets.get_Count( );

for ( int i=1;i<=numSheets;i++ )
{
wSheet = Sheets.get_Item( COleVariant( (short)(i) ) );
wSheet.Activate( );
tmpName = wSheet.get_Name();
//AfxMessageBox( tmpName.c_str() );
if ( ( tmpName != "VE_Dump" ) && ( tmpName != "Summary"
) )
sheetNames.push_back( tmpName );
}

sheetNames.sort( );
//trial to see it it works
wSheet = Sheets.get_Item( COleVariant( (short)(1) ) );
wSheet.Move( COleVariant( (short)(3) ), covOptional );




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
How to put worksheets in Alphabetical order? LiveUser Excel Discussion (Misc queries) 6 January 16th 08 03:45 PM
Arranging words in alphabetical order Leonard Excel Discussion (Misc queries) 2 May 23rd 07 03:58 PM
How do I put worksheets in alphabetical order DLee New Users to Excel 2 July 27th 05 09:16 PM
How do I put worksheets in alphabetical order DLee Excel Worksheet Functions 1 July 27th 05 08:41 PM
Worksheets in alphabetical order Mark Excel Programming 3 November 28th 03 10:24 AM


All times are GMT +1. The time now is 10:38 AM.

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"