VBA to add new Sheets

DrH100

Board Regular
Joined
Dec 30, 2011
Messages
78
Good evening all

I have some code which i have borrowed to add some new sheets to a workbook based on the contents of column a. The code is

HTML:
Sub AddSheets()
Dim cell As Excel.Range
Dim wsWithSheetNames As Excel.Worksheet
Dim wbToAddSheetsTo As Excel.Workbook

Set wsWithSheetNames = ActiveSheet
Set wbToAddSheetsTo = ActiveWorkbook
For Each cell In wsWithSheetNames.Range("A2:A5")
    With wbToAddSheetsTo
        .Sheets.Add after:=.Sheets(.Sheets.Count)
        On Error Resume Next
        ActiveSheet.Name = cell.Value
        If Err.Number = 1004 Then
          Debug.Print cell.Value & " already used as a sheet name"
        End If
        On Error GoTo 0
    End With
Next cell
End Sub

which works to a point but what I would like to do is only create the new tab if

a) there is something in the cell in column a (ie it might only be a1:a15 or the next time i run it it might be a1:a30

At the moment I get a lot of sheets named sheet 16, 17, 18 etc etc and it is creating too many but ti need the potential to add more if needed.

and

b) if that tab does not already exist.

any help amending this code would be great or pointing in a different direction.

Thanks for any help in advance
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Rich (BB code):
Sub AddSheets()
Dim cell As Excel.Range
Dim wsWithSheetNames As Excel.Worksheet
Dim wbToAddSheetsTo As Excel.Workbook
Dim lr As Long: lr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Dim found As Boolean: found = False

Set wsWithSheetNames = ActiveSheet
Set wbToAddSheetsTo = ActiveWorkbook
For Each cell In wsWithSheetNames.Range("A2:A" & lr)
    found = False
    With wbToAddSheetsTo
        For Each sh In Worksheets
            If sh.Name = CStr(cell.Value) Then
                found = True
                Exit For
            End If
        Next sh
        If Not found And cell.Value <> "" Then
            On Error Resume Next
            .Sheets.Add after:=.Sheets(.Sheets.Count)
            ActiveSheet.Name = cell.Value
        End If
    End With
Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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