Check if Sheet exists before adding new.

shaggyforum

New Member
Joined
Feb 26, 2013
Messages
11
Hi this is my first post here, after looking for a bit I didn't find an answer so I was hoping someone out there could help me.

I've got a macro, it needs to add a new sheet unless the sheet already exists. If it exists it should ignore trying to add otherwise add the sheet.

Sounds simple but I can't get it.

What I have is

Code:
If Sheets("Accounts") Is Nothing Then
    Worksheets.Add(After:=Worksheets(Worksheets.count)).Name = "Accounts"
 End If

But it needs an On Error Resume Next which I don't want. Seems like there should be an easy solution that doesn't require much code but is some VBA command I'm not familiar with. Thanks very much for your time.
 
Hi

You could od it like this:

Code:
Dim ws As Worksheet

On Error Resume Next
Set ws = Worksheets("Accounts")
On Error Goto 0

If ws Is Nothing Then Worksheets.Add(After:=Worksheets(Worksheets.count)).Name = "Accounts"
 
Upvote 0
Code:
bsheetexists = false
for i = 1 to sheets.count
  if sheets(i).name = "Accounts" then BSheetExists = True
next i
if bsheetexists = false then
  worksheets.add(after:=worksheets(worksheets.count)).name = "Accounts"
endif
 
Upvote 0
Code:
bsheetexists = false
for i = 1 to sheets.count
  if sheets(i).name = "Accounts" then BSheetExists = True
next i
if bsheetexists = false then
  worksheets.add(after:=worksheets(worksheets.count)).name = "Accounts"
endif

Thanks very much, that worked great.
 
Upvote 0

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