ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Clear workbook (https://www.excelbanter.com/excel-programming/423334-clear-workbook.html)

Lucile

Clear workbook
 
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!

Dave Peterson

Clear workbook
 
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

Lucile wrote:

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!


--

Dave Peterson

Gary''s Student

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

Sub cleanup()
Worksheets.Add befo=Worksheets(1)
Worksheets.Add befo=Worksheets(1)
Worksheets.Add befo=Worksheets(1)
n = Worksheets.Count
For i = n To 4 Step -1
Application.DisplayAlerts = False
Sheets(i).Delete
Next
End Sub

--
Gary''s Student - gsnu200830


"Lucile" wrote:

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!


Lucile

Clear workbook
 
Excellent!
Thank you very much!

"Gary''s Student" wrote:

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

Sub cleanup()
Worksheets.Add befo=Worksheets(1)
Worksheets.Add befo=Worksheets(1)
Worksheets.Add befo=Worksheets(1)
n = Worksheets.Count
For i = n To 4 Step -1
Application.DisplayAlerts = False
Sheets(i).Delete
Next
End Sub

--
Gary''s Student - gsnu200830


"Lucile" wrote:

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!


Rick Rothstein

Clear workbook
 
For future reference, these 3 lines...

Worksheets.Add befo=Worksheets(1)
Worksheets.Add befo=Worksheets(1)
Worksheets.Add befo=Worksheets(1)


can be replaced by this single line...

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

--
Rick (MVP - Excel)


"Gary''s Student" wrote in message
...
Better to start with fresh, clean, sheets rather than try to launder the
old
ones:

Sub cleanup()
Worksheets.Add befo=Worksheets(1)
Worksheets.Add befo=Worksheets(1)
Worksheets.Add befo=Worksheets(1)
n = Worksheets.Count
For i = n To 4 Step -1
Application.DisplayAlerts = False
Sheets(i).Delete
Next
End Sub

--
Gary''s Student - gsnu200830


"Lucile" wrote:

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!




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

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