worksheet if...then

excelmacro

Board Regular
Joined
Apr 8, 2002
Messages
50
I need to create a macro that will search my workbook for sheet "Data" and if there is no sheet called "Data" then create a new sheet and name it Data. If Data is present then the macro does nothing. Any suggestions?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
The routine Add_Data_Sheet() calls a private function SheetExists, so both are required.

Code:
Sub Add_Data_Sheet()

If SheetExists("Data") = False Then
    Worksheets.Add After:=Sheets(ThisWorkbook.Worksheets.Count)
    ActiveSheet.Name = "Data"
End If

End Sub

Private Function SheetExists(sheetname) As Boolean
Dim abc As Object
On Error Resume Next
Set abc = ActiveWorkbook.Sheets(sheetname)
If Err = 0 Then SheetExists = True _
Else SheetExists = False
End Function
 
Upvote 0
I cannot replicate your error.

Try to rewrite it as:

Code:
If Err = 0 Then
    SheetExists = True
Else
    SheetExists = False
End If
 
Upvote 0
I *can* replicate it.

You need to insert an underscore at the end of the first line, as this is actually only one line.

If Err = 0 Then SheetExists = True Else SheetExists = False
This message was edited by Jay Petrulis on 2002-05-03 14:29
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,183
Members
448,872
Latest member
lcaw

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