Create worksheet if worksheet does not exist

bisse2000

New Member
Joined
Dec 5, 2012
Messages
2
Hi,
I am new to the forum, but have been using it quite a lot to learn more about VBA.
I am trying to write some code that will check a range of names within a worksheet and if there is a sheet with a name from the range excel should ignore it and move on to the next range. If no such worksheet exists it should create it. When the code encounters a blank range, it should stop the code.

I have tried several different variations, and either I can't make the "check if exist" statement to work, or I am having problems with it not working for more than one loop. After reading previous posts on this forum, I have tried with err.clear, next ws in worksheet and all types of codes but I can not make it work.

When I am running the code, it will stop on second loop at [If (Worksheets(rangename).Name <> "") Then] and give me a runtime error 9 - subscript out of range.
I am so close but have spent most of yesterday figuring out what is probably an easy solution :mad: , so now I thought I would ask the experts :).
This is the code:

Sub CreateSections()

Dim i As Integer
Dim rangename As String
Dim Newsheet As String
Dim Nextrow As String

Application.ScreenUpdating = False
Sheets("Example").visible = True
Application.DisplayAlerts = False

For i = 8 To 37
rangename = Sheets("reporting dashboard").Range("A" & i)
On Error GoTo Newsheet
If (Worksheets(rangename).Name <> "") Then
GoTo Nextrow
Newsheet:
If Not rangename = "" Then
Sheets("Example").Copy before:=Sheets("example")
ActiveSheet.Name = rangename
ActiveSheet.Range("C2") = rangename
ActiveSheet.Range("C2").Select
rangename = Replace(rangename, " ", "_", 1)
ActiveWorkbook.Names.Add Name:=rangename & "_", RefersTo:=Selection
Err.Clear
End If

End If
Nextrow:
Err.Clear
Next i
Sheets("Example").visible = False
Sheets("Reporting Dashboard").Select
MsgBox "Sections Generated", vbInformation
End Sub

Thanks for your help
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)


Hi, thanks for the help.
After looking a bit further into this, it turns out is was my error handling that needed some changes. Once I got that sorted the code worked. :cool:

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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