Help! My Code is terrible and i cant get it to fix

kadain

New Member
Joined
May 19, 2022
Messages
22
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi Guy/Gals

ive been trying to run a code to delete Specific Sheets From a workbook but unfortunailty it wasn't as easy i first thought.
i need it to delete a sheet called Workspace or Workspace NW and both if both are present
my code keep returning back an error if only workpspace if present but still deletes it
the code willl delete both if there

and the error is at
Worksheets("Workspace NW").delete
at the bottom

VBA Code:
 Application.DisplayAlerts = False                                 'Search tabs for Refence and delete else find other refrence
 Dim check As Boolean
For Each Sheet In Worksheets
If Sheet.Name Like "Workspace" Then check = True: Exit For
Next
If check = True Then
Worksheets("Workspace").delete
Else


For Each Sheet In Worksheets
If Sheet.Name Like "Workspace NW" Then check = True: Exit For
Next
If check = True Then
Worksheets("Workspace NW").delete
Else
Application.DisplayAlerts = True

Exit Sub
End If
End If

Application.DisplayAlerts = False                                  ' search for refrence and delete
For Each Sheet In Worksheets
If Sheet.Name Like "Workspace" Then check = True: Exit For
Next
If check = True Then
Worksheets("Workspace NW").delete
Else
Sheets("Homepage").Select
Application.DisplayAlerts = True
Exit Sub
End If

i know ive kind of over done the coding on this but i havent been doing this very long and sure there proably a much simplier format to do it but sadly am not that good yet.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
How about
VBA Code:
Sub kadain()
   Dim Ws As Worksheet

   Application.DisplayAlerts = False
   For Each Ws In Worksheets
      If Ws.Name Like "Workspace*" Then Ws.Delete
   Next Ws
   Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub kadain()
   Dim Ws As Worksheet

   Application.DisplayAlerts = False
   For Each Ws In Worksheets
      If Ws.Name Like "Workspace*" Then Ws.Delete
   Next Ws
   Application.DisplayAlerts = True
End Sub
Thats the Ticket, Thank you so much
orignally thats what i had with out the *
does the * make a big Diffrence
 
Upvote 0
Yes it's a wildcard so it looks for any sheets that start with "Workspace"
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top