Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Delete sheets code error requires object


I found this code that makes a new workbook with one sheet, modified it and deleted some unwanted stuff and added code to do two sheets. Changed the
'ws name' lines to include a date and the format.

The code near the bottom to get rid of any sheet not "named as" was originally for just one new sheet. If s.Name < ws1.Name Then s.Delete

I added for the second sheet but now those lines error out with Object Required.

The Application.DisplayAlerts = False both before AND afterwards is as the code was displayed. This is puzzling to me. Don't know if it is a typo or for real.

When I noticed it I changed the last one to True, but still get the error.
I want to get rid of the default sheet1 to sheet3.

I'm sure there are some Dim statement I don't need, but was going to wait until code worked as I wanted, then dismiss the un-needed ones.

Thanks,
Howard

Option Explicit

Sub aBook()

Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim s As Variant
Dim btn As OLEObject
Dim lLines As Long

Application.ScreenUpdating = False
Set wb = Workbooks.Add

Set ws1 = Worksheets.Add
ws1.Name = Format(Date, "[$-409]d-mmm-yy;@") & " First Choice"

Set ws2 = Worksheets.Add
ws2.Name = Format(Date, "[$-409]d-mmm-yy;@") & " Second Choice"

' Application.DisplayAlerts = False
' For Each s In wb.Worksheets
' If s.Name < ws1.Name Then s.Delete
' If s.Name < ws2.Name Then s.Delete
' Next s
' Application.DisplayAlerts = False

Application.ScreenUpdating = True
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Delete sheets code error requires object

Hi Howard,

Am Mon, 2 Dec 2013 12:02:53 -0800 (PST) schrieb Howard:

I found this code that makes a new workbook with one sheet, modified it and deleted some unwanted stuff and added code to do two sheets. Changed the
'ws name' lines to include a date and the format.

The code near the bottom to get rid of any sheet not "named as" was originally for just one new sheet. If s.Name < ws1.Name Then s.Delete

I added for the second sheet but now those lines error out with Object Required.


the array s is not filled with the sheet names.
But it is easier to do it with For each wsh in ActiveWorkbook.Worksheets

Try:

Sub aBook()

Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim wsh As Worksheet
Dim btn As OLEObject
Dim lLines As Long

Application.ScreenUpdating = False

Set wb = Workbooks.Add
Set ws1 = Worksheets.Add
ws1.Name = Format(Date, "[$-409]d-mmm-yy;@") & " First Choice"

Set ws2 = Worksheets.Add
ws2.Name = Format(Date, "[$-409]d-mmm-yy;@") & " Second Choice"

Application.DisplayAlerts = False
For Each wsh In ActiveWorkbook.Worksheets
If wsh.Name < ws1.Name And wsh.Name < ws2.Name Then wsh.Delete
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Regards
Claus B.
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Delete sheets code error requires object

On Monday, December 2, 2013 12:18:44 PM UTC-8, Claus Busch wrote:
Hi Howard,



Am Mon, 2 Dec 2013 12:02:53 -0800 (PST) schrieb Howard:



I found this code that makes a new workbook with one sheet, modified it and deleted some unwanted stuff and added code to do two sheets. Changed the


'ws name' lines to include a date and the format.




The code near the bottom to get rid of any sheet not "named as" was originally for just one new sheet. If s.Name < ws1.Name Then s.Delete




I added for the second sheet but now those lines error out with Object Required.




the array s is not filled with the sheet names.

But it is easier to do it with For each wsh in ActiveWorkbook.Worksheets



Try:



Sub aBook()



Dim wb As Workbook

Dim ws1 As Worksheet, ws2 As Worksheet

Dim wsh As Worksheet

Dim btn As OLEObject

Dim lLines As Long



Application.ScreenUpdating = False



Set wb = Workbooks.Add

Set ws1 = Worksheets.Add

ws1.Name = Format(Date, "[$-409]d-mmm-yy;@") & " First Choice"



Set ws2 = Worksheets.Add

ws2.Name = Format(Date, "[$-409]d-mmm-yy;@") & " Second Choice"



Application.DisplayAlerts = False

For Each wsh In ActiveWorkbook.Worksheets

If wsh.Name < ws1.Name And wsh.Name < ws2.Name Then wsh.Delete

Next

Application.DisplayAlerts = True

Application.ScreenUpdating = True

End Sub



Regards

Claus B.



That clears that up nicely.

Thanks.
Howard
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
(Error 1004) Object Defined Error ...simple code Robert Crandal Excel Programming 1 January 12th 10 10:31 PM
Delete object code Marie Bayes Excel Discussion (Misc queries) 1 April 29th 09 03:41 PM
Charts.Add error '1004' Method 'Add' of object 'Sheets' failed Corey Charts and Charting in Excel 1 December 28th 06 02:15 PM
Error 1004 Method 'Add'of Object Sheets failed ExcelMonkey[_190_] Excel Programming 10 March 29th 05 01:52 PM


All times are GMT +1. The time now is 06:11 PM.

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

About Us

"It's about Microsoft Excel"