Clear workbook

  • Thread starter Thread starter Lucile
  • Start date Start date
L

Lucile

Hi all,

I need to add a piece of code at the beginning of my program that checks if
the workbook is empty or not (I just want 3 blank sheets). And if it is not
empty (charts, sheets with data...) it needs to clear/delete everything and
keep only 3 blank sheets....

Thanks a lot!
 
Why not just start a new workbook with 3 worksheets?

Option Explicit
Sub testme()
Dim NewWkbk As Workbook
Dim CurSheetsInWorkbook As Long

CurSheetsInWorkbook = Application.SheetsInNewWorkbook
Application.SheetsInNewWorkbook = 3
Set NewWkbk = Workbooks.Add
Application.SheetsInNewWorkbook = CurSheetsInWorkbook

'then work with newwkbk

End Sub
 
Better to start with fresh, clean, sheets rather than try to launder the old
ones:

Sub cleanup()
Worksheets.Add before:=Worksheets(1)
Worksheets.Add before:=Worksheets(1)
Worksheets.Add before:=Worksheets(1)
n = Worksheets.Count
For i = n To 4 Step -1
Application.DisplayAlerts = False
Sheets(i).Delete
Next
End Sub
 
Excellent!
Thank you very much!

Gary''s Student said:
Better to start with fresh, clean, sheets rather than try to launder the old
ones:

Sub cleanup()
Worksheets.Add before:=Worksheets(1)
Worksheets.Add before:=Worksheets(1)
Worksheets.Add before:=Worksheets(1)
n = Worksheets.Count
For i = n To 4 Step -1
Application.DisplayAlerts = False
Sheets(i).Delete
Next
End Sub
 
For future reference, these 3 lines...
Worksheets.Add before:=Worksheets(1)
Worksheets.Add before:=Worksheets(1)
Worksheets.Add before:=Worksheets(1)

can be replaced by this single line...

Worksheets.Add Before:=Worksheets(1), Count:=3
 

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

Back
Top