How can I add another loop to insert worksheets based on a list?

kbj0109

New Member
Joined
Mar 28, 2015
Messages
26
I want to add a loop that create worksheets with the same name on the list, if the workbook does not have the same named worksheets in a list.
How would I add it on this code?
Can anyone do it..?

Sub DeleteSheet()
Dim WS As Worksheet, SheetsToKeep As String
Const SheetNameWithList As String = "List"
''''First worksheet has the list, and Cell A1 is the title of list
SheetsToKeep = "/" & SheetNameWithList & "/" & Join(Application.Transpose( _
Range("A2", Cells(Rows.Count, "A").End(xlUp)).Value), "/") & "/"
Application.DisplayAlerts = False
On Error Resume Next
For Each WS In ThisWorkbook.Worksheets
If InStr(SheetsToKeep, "/" & WS.Name & "/") = 0 Then WS.Delete
Next
On Error GoTo 0
Application.DisplayAlerts = True




End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi,

I would write it like this:
Code:
Sub CreateAndDeleteSheet()
    
    Dim r As Range
    Dim rList As Range
    Dim wsFound As Boolean
    Dim wsList As Worksheet
    
    With ThisWorkbook

        Set wsList = .Worksheets("List")
    
        With wsList
            Set rList = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
        End With
        
        For Each WS In .Worksheets
            wsFound = False
            For Each r In rList
                If (WS.Name = r.Value) Or (WS.Name = wsList.Name) Then
                    wsFound = True
                    Exit For
                End If
            Next
            If Not wsFound Then
                Application.DisplayAlerts = False
                WS.Delete
                Application.DisplayAlerts = True
            End If
        Next
        
        For Each r In rList
            wsFound = False
            For Each WS In .Worksheets
                If WS.Name = r.Value Then
                    wsFound = True
                    Exit For
                End If
            Next
            If Not wsFound Then
                Set WS = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
                WS.Name = r.Value
            End If
        Next
        
    End With
    
End Sub

This macro does both the delete and create worksheet steps.

First it goes through the list of worksheets in the workbook and checks to see if it is in the list in column A. If it is not then it deletes the worksheet.

Second, it goes through the list of names in column A and checks that against the current list of worksheets in the workbook. Any missing ones are created and placed at the end.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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