View Single Post
  #3   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?

On 22 Feb, 12:21, joel wrote:
first, I hatte the ON Error statements. *I think it bad programming
practive to use an error to find a non-error condition. *To test for
sheets I often use a for loop

LastSht = sheets.count
for shtCount = LastSht to 1 step -1
Set Sht = sheets(Shtcount)
If sht.name < "Options" And sht.name < "xPlot" Then
'add your code here *
end if
next ShtCount

To get an Array of sheet names use the code below. *there isn't a good
way of getting an array of constants.

ShtNames = Array("Sheet1","Sheet3",'Sheet5")
for each ShtName in ShtNames
Set Sht = sheets(ShtNames)

nextt ShtName

--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread:http://www.thecodecage.com/forumz/sh...d.php?t=181370

Microsoft Office Help


You're right, Joel. The same job could be done without using On Error.
Here's my new code

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

I don't use Set because with the construct For Each ..., sht is
automatically set to each element of the collection Sheets, in
succession. Apart from this, it looks like our codes are quite
similar, so I'll go for this solution. Thanks

Best Regards

deltaquattro