ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to program to insert / delete any worksheet? (https://www.excelbanter.com/excel-programming/406964-how-program-insert-delete-any-worksheet.html)

Eric

How to program to insert / delete any worksheet?
 
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


joel

How to program to insert / delete any worksheet?
 
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


"Eric" wrote:

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



All times are GMT +1. The time now is 01:02 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com