Error handling if worksheet does not exist

charliec83`

New Member
Joined
Oct 30, 2017
Messages
2
Hi,

I have built a macro that asks a user to input a development name into an input box. Then the code searches to see if there is a sheet with the name the user enters and deletes that worksheet. It then also removes a range of data on the summary sheet. The code I have so far is as follows:

Code:
Sub Delete_Apartment()
    
    Dim Dname As Variant
    Dim wks As Worksheet
    Dim SearchRow As Integer
    Dim DelSelection As Range
    
        
    Dname = Application.InputBox(Prompt:="Enter Development to delete")
    If Dname = False Then Exit Sub
    
    Sheets(Dname).Delete
    
    SearchRow = 25
    
    While SearchRow < 500
    
    If Range("B" & CStr(SearchRow)).Value = Dname Then
    Range("B" & CStr(SearchRow)).Offset(-2).Resize(8, 17).Delete
    
    End If
    
    SearchRow = SearchRow + 1
    
    Wend
    
        
End Sub

The only thing I can't work out is how to return a simple error message if the name they type into the input box does not match to a sheet in this workbook (for example they mis-spell it).

Thank you in advance for your help.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi & welcome to the board.
Try this
Code:
Sub Delete_Apartment()
    
    Dim Dname As Variant
    Dim wks As Worksheet
    Dim SearchRow As Integer
    Dim DelSelection As Range
    
        
    Dname = Application.InputBox(Prompt:="Enter Development to delete")
    If Dname = False Then Exit Sub
    
    On Error GoTo Xit
    Sheets(Dname).Delete
    On Error GoTo 0
    
    SearchRow = 25
    
    While SearchRow < 500
    
    If Range("B" & CStr(SearchRow)).Value = Dname Then
    Range("B" & CStr(SearchRow)).Offset(-2).Resize(8, 17).Delete
    
    End If
    
    SearchRow = SearchRow + 1
    
    Wend
    
Exit Sub
Xit:
MsgBox "Sheet name " & Dname & " not found"
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,231
Members
448,951
Latest member
jennlynn

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