Custom Message instead of error msg

Shaido

New Member
Joined
Feb 24, 2011
Messages
4
Hi

I've created a macro within excel 2010 that adds a new tab called 'Data' into my worksheet. The main analysis of the worksheet is now run from this 'Data' worksheet.

My problem is if the 'Data' worksheeet has been created already, and the Macro that creates it is run again, by accident, I get the 'End' or 'Debug' msg box, and am also thrown into the VBA screen.

Is there a way I can bring up a Msg that says "Data sheet already created" and also then ends the Macro? And without being thrown into the VBA screen?

Thanks.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome to the board.

Assuming the sheet is ALWAYS called "Data" try:
Code:
Sub TestForDataSheet ()
Dim i as Long
For i = 1 to Worksheets.Count
     If sheets(i).Name = "Data" Then
        MsgBox "Data sheet already created, macro exiting"
        Exit Sub
     End If
Next i
End Sub
 
Upvote 0
Try adding this function

Code:
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function

You can then use

Code:
If WorksheetExists("Data") Then
    MsgBox "Already exists", vbInformation
    Exit Sub
End If
'
'code to add sheet
 
Upvote 0
Code:
Dim wsName$
On Error Resume Next
wsName = Sheets("Data").Name
On Error GoTo 0
If wsName <> "" Then
    MsgBox "Data sheet already created."
    Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,965
Members
449,201
Latest member
Jamil ahmed

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