ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How can I check a collection (e.g. worksheets) collection to be empty? (https://www.excelbanter.com/excel-programming/441027-how-can-i-check-collection-e-g-worksheets-collection-empty.html)

Michiel via OfficeKB.com

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


Dave Peterson

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

Michiel via OfficeKB.com

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



All times are GMT +1. The time now is 02:25 PM.

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