Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default How can I check a collection (e.g. worksheets) collection to be empty?

I have a function that checks if a worksheet exists by For Next-ing through
the Worksheets collection.


Function fncSheetExist(SheetName As String) As Boolean
Dim sh As Worksheet

fncSheetExist = False
For Each sh In Worksheets
If sh.Name = SheetName Then fncSheetExist = True
Next sh
End Function

In the case there is no workbook open, this code will raise an error (1004)
since the Worksheets collection does not exist or is empty.

How can I test for that upfront? Without implementing error handling.
These do NOT work:
- If worksheets.count = 0 ....
- If worksheets = Nothing .....

Any ideas?

Thanks!

--
Message posted via http://www.officekb.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default How can I check a collection (e.g. worksheets) collection to beempty?

It looks like you're using the activeworkbook.

Maybe you could check before you use the function:

if activeworkbook is nothing then
'don't do it
else
'do the function
end if

"Michiel via OfficeKB.com" wrote:

I have a function that checks if a worksheet exists by For Next-ing through
the Worksheets collection.

Function fncSheetExist(SheetName As String) As Boolean
Dim sh As Worksheet

fncSheetExist = False
For Each sh In Worksheets
If sh.Name = SheetName Then fncSheetExist = True
Next sh
End Function

In the case there is no workbook open, this code will raise an error (1004)
since the Worksheets collection does not exist or is empty.

How can I test for that upfront? Without implementing error handling.
These do NOT work:
- If worksheets.count = 0 ....
- If worksheets = Nothing .....

Any ideas?

Thanks!

--
Message posted via http://www.officekb.com


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default How can I check a collection (e.g. worksheets) collection to be empty?

THAT IS IT!

Thanks Dave!

M

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/201003/1

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
Check a collection for objects that may or may not exist within it NateBuckley Excel Programming 2 May 14th 08 01:50 PM
Removing a collection item via key name check [email protected] Excel Programming 3 June 30th 06 03:10 PM
Workbooks collection is empty even though the some of the document is open [email protected] Excel Programming 12 December 28th 05 10:47 AM
Check for an item in a collection (without looping) Rob Excel Programming 2 February 10th 05 11:18 AM
worksheets collection... find... mark kubicki Excel Programming 2 February 23rd 04 08:02 PM


All times are GMT +1. The time now is 05:59 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"