D
deltaquattro
Hi,
I've written a VBA code which, as an output, adds many sheets to the
current workbook. Before running the code, I'd like to delete all old
sheets in the workbook, save for the worksheets which the user needs
to provide input to the code. So I wrote the following snippet:
Sub DeleteOutputSheets()
Dim sht As Object
' Delete old sheets TODO
For Each sht In Sheets
If sht.name <> "Options" And sht.name <> "xPlot" Then
'Delete worksheet if it exists
On Error Resume Next
Application.DisplayAlerts = False
Sheets(sht.name).Delete
On Error GoTo 0
End If
Next
Set sht = Nothing
' TODO
End Sub
Questions follow:
1. Would you have coded the thing in the same way, e.g. using a For...
Each construct and an If , or do you think there's something quicker/
more robust/etc.?
2. I hardcoded the name of the sheets which must be deleted, which I
reckon Is Not A Good Thing. I'd like to pass an array of strings
InputSheets to the subroutine which contains the name of the sheets to
be spared from deletion (would it be better to pass a collection
containing the sheets?). However, the sheets are always the same, so
I'd like to define InputSheets as a Const in the declaration section
of the module. Alas, that's not possible because Const arrays are not
allowed. What do you suggest? Do I declare InputSheets as a Const and
then I allocate the names in a routine which must be launched at the
very start of the code?
Thanks in advance for your help and feel free to add comments/
suggestions/questions on coding style, structure, etc. if you have
any.
Best Regards
deltaquattro
I've written a VBA code which, as an output, adds many sheets to the
current workbook. Before running the code, I'd like to delete all old
sheets in the workbook, save for the worksheets which the user needs
to provide input to the code. So I wrote the following snippet:
Sub DeleteOutputSheets()
Dim sht As Object
' Delete old sheets TODO
For Each sht In Sheets
If sht.name <> "Options" And sht.name <> "xPlot" Then
'Delete worksheet if it exists
On Error Resume Next
Application.DisplayAlerts = False
Sheets(sht.name).Delete
On Error GoTo 0
End If
Next
Set sht = Nothing
' TODO
End Sub
Questions follow:
1. Would you have coded the thing in the same way, e.g. using a For...
Each construct and an If , or do you think there's something quicker/
more robust/etc.?
2. I hardcoded the name of the sheets which must be deleted, which I
reckon Is Not A Good Thing. I'd like to pass an array of strings
InputSheets to the subroutine which contains the name of the sheets to
be spared from deletion (would it be better to pass a collection
containing the sheets?). However, the sheets are always the same, so
I'd like to define InputSheets as a Const in the declaration section
of the module. Alas, that's not possible because Const arrays are not
allowed. What do you suggest? Do I declare InputSheets as a Const and
then I allocate the names in a routine which must be launched at the
very start of the code?
Thanks in advance for your help and feel free to add comments/
suggestions/questions on coding style, structure, etc. if you have
any.
Best Regards
deltaquattro