How to program to insert / delete any worksheet?

E

Eric

Does anyone have any suggestions on how to program excel to insert / delete
any worksheet?
For example,
Under column A, there is a list of numbers, such as
1,4,9, ...
If there is no existing worksheet named "1", then insert a new worksheet
named under "1", else do nothing.
Repeat this process until the end of the list.

For checking, if 3 is not included under column A and there is an existing
worksheet named under "3", then delete it.
Repeat this process until the last worksheet checked.

Does anyone have any suggestions on how to code it in excel?
Thanks in advance for any suggestions
Eric
 
J

Joel

Make sure the in both functions you change With Sheets("Sheet1") to be the
sheet with the sheet names.

Sub AddSheets()

RowCount = 1
With Sheets("Sheet1")
Do While .Range("A" & RowCount) <> ""
found = False
For Each sht In ThisWorkbook.Sheets
If .Range("A" & RowCount) = sht.Name Then
found = True
Exit For
End If
Next sht
If found = False Then
Worksheets.Add _
after:=ThisWorkbook.Sheets(Sheets.Count)
ActiveSheet.Name = .Range("A" & RowCount)
End If
RowCount = RowCount + 1
Loop
End With
End Sub
Sub RemoveSheets()

Application.DisplayAlerts = False
For Each sht In ThisWorkbook.Sheets
With Sheets("Sheet1")
If sht.Name <> .Name Then
Set c = .Columns("A:A").Find(what:=sht.Name, _
LookIn:=xlValues, lookat:=xlWhole)
If c Is Nothing Then
Sheets(sht.Name).Delete
End If
End If
End With
Next sht
Application.DisplayAlerts = False
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top