Delete worksheet if it exists and create a new one
excel, vba
Solution
Remove the `End` statement, your code terminates after finding and deleting the worksheet `asdf`.
For Each ws In Worksheets
If ws.Name = "asdf" Then
Application.DisplayAlerts = False
Sheets("asdf").Delete
Application.DisplayAlerts = True
End If
Next
Sheets.Add(After:=Sheets(Sheets.count)).Name = "asdf"
Problem
I want to look through my Excel worksheets and find a sheet with a certain name and delete that sheet if it is found. Afterwards I want to create a sheet after all existing sheets with that name. My code is as follows: ``` For Each ws In Worksheets If ws.Name = "asdf" Then Application.DisplayAlerts = False Sheets("asdf").Delete Application.DisplayAlerts = True End End If Next Sheets.Add(After:=Sheets(Sheets.count)).Name = "asdf" ``` However this doesn't do both of these actions in one run of the code. If the sheet already exists it will simply delete the sheet and not make a new one like I want it to. I need to run it again for it to create a new one. How do I fix my code to delete the old sheet if it exists and create a new one?