
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete Specific Sheet If Exists in Workbook
Microsoft Excel is a sophisticated programme that allows users to efficiently organise, analyse, and manipulate data. When working with big workbooks including several sheets, the ability to delete certain sheets if they are no longer required can be useful.
This tutorial will teach you how to create a simple VBA (Visual Basic for Applications) code that checks for the presence of a specific sheet in an Excel workbook and deletes it if it is detected.
Deleting a Specific Sheet if It Exists in a Workbook
Here we will first create a VBA module and then run it to complete the task. So let us see a simple process to know how you can delete a specific sheet if it exists in a workbook in Excel.
Step 1
Consider any Excel workbook where you have multiple sheets.
First, right?click on the sheet name and select View code to open the VBA application.
Right click > View code.
Step 2
Then click on Insert, select Module, and copy the below code into the text box.
Insert > Module > Copy.
Example
Sub Test() Dim xWs As Worksheet Dim sheetName As String sheetName = Application.InputBox("Input Sheet Name:", "Delete Specific Sheet", _ "sheet1", , , , , 2) Application.DisplayAlerts = False Err.Clear On Error Resume Next Set xWs = Sheets(sheetName) If Err <> 0 Then MsgBox "The'" & sheetName & "'" & "does not exist!", vbInformation, "Delete Specific Sheet" Exit Sub Else xWs.Delete MsgBox "The'" & sheetName & "'" & "has been deleted!", vbInformation, "Delete Specific Sheet" End If Application.DisplayAlerts = True End Sub
Step 3
Then click F5 to run the module, specify the sheet you want to delete, and click Ok. Then you can see that the message will be displayed.
F5 > Specify Name > Ok.
Conclusion
In this tutorial, we have used a simple example to demonstrate how you can delete a specific sheet if it exists in a workbook in Excel to highlight a particular set of data.