View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
deltaquattro deltaquattro is offline
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