Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default 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!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default 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!

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default 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!

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default 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!


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Still need help - Clear a cell upon workbook Close Gary Excel Discussion (Misc queries) 2 November 24th 09 08:40 PM
clear cells when workbook is opened Wanna Learn Excel Discussion (Misc queries) 6 May 15th 09 06:54 PM
How to clear links to external workbook Wilbur[_9_] Excel Programming 2 March 15th 06 02:10 AM
Trying get macro to clear cells when workbook is opened. kcdonaldson Excel Programming 1 December 6th 05 04:44 PM
clear data from workbook lynn Excel Worksheet Functions 1 June 27th 05 07:46 PM


All times are GMT +1. The time now is 09:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"