Do I have to reset the "On Error" condition or am I doing something else wrong?
I am trying to run through a series of specific worksheets amoung many worksheets. If the sheet exists then clear the entire worksheet. If the sheet does not exist then create it.
It creates the first missing worksheet without any problem. When it encounters the second missing sheet it never fails the "If Not wsSheet Is Nothing Then" test. Meaning it never enters the "Else" condition again. So it does not create any of the missing sheets and crashes on "Worksheets(sSheetTo).Cells.Clear" when it tries to clear the missing worksheet.
Do I have to reset the "On Error" condition or am I doing something else wrong?
Respectfully,
Craigm
'===========================================
Dim wsSheet As Worksheet 'For worksheet exists only
sSheetFrom = "Inventory"
For k = 1 To 9
If k = 1 Then
sSheetTo = "Auto"
ElseIf k = 2 Then
sSheetTo = "Trucks"
ElseIf k = 3 Then
sSheetTo = "Vans"
ElseIf k = 4 Then
sSheetTo = "Boats"
ElseIf k = 5 Then
sSheetTo = "Airplanes"
ElseIf k = 6 Then
sSheetTo = "Motorcycles"
ElseIf k = 7 Then
sSheetTo = "Trailers"
ElseIf k = 8 Then
sSheetTo = "SUVs"
ElseIf k = 9 Then
sSheetTo = "Unknown"
End If
''''We need to run through this and clear all worksheets.
'If it does EXIST then clear its contents out
'If the passed in WorkSheet does not EXIST then create it.
On Error Resume Next
Set wsSheet = Worksheets(sSheetTo)
On Error GoTo 0
If Not wsSheet Is Nothing Then'..................It does Exist
wsSheet.Activate
Worksheets(sSheetTo).Cells.Clear
Else'...........................................................It does Not exist
Worksheets.Add.Name = (sSheetTo)
End If
Next k
Bookmarks