LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default Workaround to define an array of constants?

Wow, Chip,

impressive as always! Thank you so much. Sorry for the late answer,
but I've been sick, and, after that, very busy to catch up with all
the work which piled up in the meantime! Just some questions, to
understand your code better:

[..]
If IsArray(SheetsToKeep) Then
* * ReDim Arr(LBound(SheetsToKeep) To UBound(SheetsToKeep))
* * On Error Resume Next
* * For N = LBound(SheetsToKeep) To UBound(SheetsToKeep)
* * * * Arr(N) = WB.Worksheets(SheetsToKeep(N)).Name
* * Next N


Ok, this converts the array SheetsToKeep of scalars/strings to an
array Arr of sheet names. Right?

Else
* * ReDim Arr(1 To 1)
* * Arr(1) = WB.Worksheets(SheetsToKeep).Name
End If
Application.DisplayAlerts = False
On Error Resume Next
For N = WB.Worksheets.Count To 1 Step -1


Is it really necessary to go from the last to the first sheet? My code
worked correctly, even though it used a For Each... which (I guess)
doesn't scan the Sheets collection in a prescribed order. I think that
the code worked all the same because it uses sheets names (which do
not change when sheets are deleted) instead than sheet number (which
do change). I rewrite my code here, just as a reference:

Sub DeleteSheets(SheetsToBePreserved() As String)
' Delete all sheets in a workbook, except the SheetsToBePreserved
Dim sht As Object, DeleteSheet As Boolean, I As Long

For Each sht In Sheets

' assume the sheet is to be deleted
DeleteSheet = True

' check if the sheet must be preserved
For I = 1 To UBound(SheetsToBePreserved)
If sht.Name = SheetsToBePreserved(I) Then
DeleteSheet = False
End If
Next I

If DeleteSheet Then
' delete sheet
Application.DisplayAlerts = False
Sheets(sht.Name).Delete
End If

Next

End Sub

* * If IsError(Application.Match(WB.Worksheets(N).Name, Arr, 0)) Then


Ah, you are using Match, because it's quicker than my using a For to
check the name of the sheet against all the names in Arr. Right?
I understand the rest of the code: if WB.Worksheets(N).Name is in Arr,
then count the number of visible sheets and if that's equal to 1, then
exit the For cycle (we cannot delete any more sheets), otherwise
delete the sheet and go to the next sheet in the Workbook.
VisibleSheetCount is declared private so that only subroutines in the
SheetsManagement module can use it.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
* * * * Excel, 1998 - 2010
Pearson Software Consulting, LLCwww.cpearson.com


Thanks again,

ciao

Sergio Rossi
 
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
Array Constants kittronald Excel Worksheet Functions 0 June 15th 11 12:04 AM
Wierd range name defined as array of constants?!? Andy Smith[_2_] Excel Programming 9 September 21st 09 09:39 PM
unions, intersections or array constants Loadmaster Excel Worksheet Functions 24 May 6th 09 08:11 PM
Can VBA use array constants Walter Briscoe Excel Programming 1 March 30th 09 02:09 PM
Help with named constants in Define Name dialog box!!!! ilona Excel Programming 3 May 1st 04 12:01 AM


All times are GMT +1. The time now is 06:53 AM.

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

About Us

"It's about Microsoft Excel"