Clear data in excel sheet & Delete More than One Worksheet

JOHATUP

New Member
Joined
May 26, 2023
Messages
22
Office Version
  1. 2013
Platform
  1. Windows
exploring and beginner on this VBA code, I was intend to combine 2 VBA code where in I clear data in "MASTER" Sheet and "MASS SENDING" sheet, the code below did work in clearing data in the stated sheets so I added code to delete few sheets(paticularly about 20-50 sheets needed to be deleted) that does not needed anymore, exept for my "MACRO" sheets, "MASTER" and "MASS SENDING" sheets. But upon running the code it still delete the exempted Sheets stated. Need help in fixing this code below.

VBA CODE:

Sub Deletion()

Sheets("MASTER").Range("A2:V1048575").ClearContents

Sheets("MASS SENDING").Range("A2:G1048575").ClearContents



Dim Ws As Worksheet



For Each Ws In ActiveWorkbook.Worksheets

If ActiveSheet.Name <> "MASTER" & "MASS SENDING" Then

Ws.Delete



End If

Next Ws


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Change references where required.
Code:
Sub Clear_All_With_Exeption()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
Select Case sh.Name
Case "Sheet1", "Sheet3", "Sheet5"    '<------ Do nothing in these Sheets
Case Else
    With sh
        .UsedRange.Offset(1).ClearContents
    End With
End Select
Next sh
End Sub

If you want to delete the Sheets from the Workbook, try this.
Code:
Sub Delete_With_Exeption()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
Select Case sh.Name
Case "Sheet1", "Sheet3", "Sheet5"    '<------ Do nothing with these Sheets
Case Else
    Application.DisplayAlerts = False
        sh.Delete
    Application.DisplayAlerts = True
End Select
Next sh
End Sub
 
Last edited:
Upvote 1
Change this

to this
VBA Code:
If Ws.Name <> "MASTER" And Ws.Name <> 'MASS SENDING' Then
Hi Kevin this one works, thank you. Do you know how to remove the "permission" when deleting each single sheets? When the code runs it ask to be confirm before deleting the sheets which may take time cause probably i might have large number of sheets to delete.
 
Upvote 0
Wrap the deletion line in false/true application display alerts like this:

VBA Code:
Application.DisplayAlerts = False
   Ws.Delete
Application.DisplayAlerts = True
 
Upvote 1
Solution
Re: "Do you know how to remove the "permission" when deleting each single sheets"
I guess no use trying to help some people as they don't try the suggested macros.
2 Days ago the solution to this was given.
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,069
Members
449,092
Latest member
ipruravindra

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