Macro to delete sheets

LearnMeExcel

Well-known Member
Joined
Aug 11, 2009
Messages
746
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi
i wrote this code
Code:
Sub DelSh()
Dim ws As Worksheet, wb As Workbook
Set wb = ActiveWorkbook
Application.DisplayAlerts = False
For Each ws In Sheets
    If ws.Name = Split(wb.Name, ".") Then
        ws.Delete
    End If
Next ws
Application.DisplayAlerts = True
End Sub

but it doesn't work
when i change this line in my code it is working
Code:
If ws.Name & ".xlsx" = wb.Name Then
what is the wrong with Split

Thanx in Advanced
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Split returns an array, not a text string.

Try:

Code:
Sub DelSh()
Dim ws As Worksheet, wb As Workbook, sName() as String
Set wb = ActiveWorkbook
Application.DisplayAlerts = False
For Each ws In Sheets
    sName = Split(wb.Name, ".")
    If ws.Name = sName(0) Then
        ws.Delete
    End If
Next ws
Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

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