Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 279
Default For Each Type mismatch

I am using Microsoft Office Excel 2003(11.8328.8329) SP3.
I like to type variables as tightly as possible.

I am building some code which is going to loop through Sheets.
To that end, I start with "For Each S in Sheets
Next S"

I start by declaring S with "Dim S" as I do not know its type.

View/Locals Window shows the type of S as Variant/Object/Sheet1.
When I replace the declaration of S with Dim S as Sheet1,
Foe Each S in Sheets gets a "Run-time error '13':" which is a "Type
Mismatch". The "best" I can do is "Dim S as Object".

When I look at the "For Each...Next Statement" help, I read
"element Required. Variable used to iterate through the elements of the
collection or array. For collections, element can only be a Variant
variable, a generic object variable, or any specific object variable.
...."

What is the definition of "specific object variable" and why does "Dim S
as Sheet1" not produce one of the right type?
I hope someone will quote from as authoritative a URL as possible.
Thanks.
--
Walter Briscoe
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default For Each Type mismatch

On Sun, 10 Apr 2011 08:48:44 +0100, Walter Briscoe wrote:

I am using Microsoft Office Excel 2003(11.8328.8329) SP3.
I like to type variables as tightly as possible.

I am building some code which is going to loop through Sheets.
To that end, I start with "For Each S in Sheets
Next S"

I start by declaring S with "Dim S" as I do not know its type.

View/Locals Window shows the type of S as Variant/Object/Sheet1.
When I replace the declaration of S with Dim S as Sheet1,
Foe Each S in Sheets gets a "Run-time error '13':" which is a "Type
Mismatch". The "best" I can do is "Dim S as Object".

When I look at the "For Each...Next Statement" help, I read
"element Required. Variable used to iterate through the elements of the
collection or array. For collections, element can only be a Variant
variable, a generic object variable, or any specific object variable.
..."

What is the definition of "specific object variable" and why does "Dim S
as Sheet1" not produce one of the right type?
I hope someone will quote from as authoritative a URL as possible.
Thanks.


I don't have a particular citation, but I'd wonder if what you are seeing has to do with the particular type of object that is Sheet1. This is not specifically defined as part of the Sheets collection. And it also seems to me that there can only be a single Sheet1 object in a workbook; so you really couldn't iterate through all of the Sheet1's, since there is only one.

If you want to iterate through all the worksheets in a workbook, you could use Dim S as Worksheet.

If you want to iterate through all of the sheets in a workbook, regardless of the type of sheet, you could do something like:

dim i as long
for i = 1 to Sheets.count
...do something to the sheet...
next i
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 279
Default For Each Type mismatch

In message of Sun, 10 Apr
2011 06:29:00 in microsoft.public.excel.programming, Ron Rosenfeld
writes
On Sun, 10 Apr 2011 08:48:44 +0100, Walter Briscoe
wrote:


[snipped why "Dim S as Sheet1: For Each S in Sheets ..." gets 1038]


I don't have a particular citation, but I'd wonder if what you are
seeing has to do with the particular type of object that is Sheet1.
This is not specifically defined as part of the Sheets collection. And
it also seems to me that there can only be a single Sheet1 object in a
workbook; so you really couldn't iterate through all of the Sheet1's,
since there is only one.


Agreed!


If you want to iterate through all the worksheets in a workbook, you
could use Dim S as Worksheet.


I should have seen that. I did not read Sheets Collection Object Help.


If you want to iterate through all of the sheets in a workbook,
regardless of the type of sheet, you could do something like:

dim i as long
for i = 1 to Sheets.count
...do something to the sheet...
next i


I am happy to do Dim S as Worksheet: for Each S in sheets ...
[If sheets contains any charts, I do not understand the data.]

Thanks.
--
Walter Briscoe
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default For Each Type mismatch

On Sun, 10 Apr 2011 14:50:19 +0100, Walter Briscoe wrote:

In message of Sun, 10 Apr
2011 06:29:00 in microsoft.public.excel.programming, Ron Rosenfeld
writes
On Sun, 10 Apr 2011 08:48:44 +0100, Walter Briscoe
wrote:


[snipped why "Dim S as Sheet1: For Each S in Sheets ..." gets 1038]


I don't have a particular citation, but I'd wonder if what you are
seeing has to do with the particular type of object that is Sheet1.
This is not specifically defined as part of the Sheets collection. And
it also seems to me that there can only be a single Sheet1 object in a
workbook; so you really couldn't iterate through all of the Sheet1's,
since there is only one.


Agreed!


If you want to iterate through all the worksheets in a workbook, you
could use Dim S as Worksheet.


I should have seen that. I did not read Sheets Collection Object Help.


If you want to iterate through all of the sheets in a workbook,
regardless of the type of sheet, you could do something like:

dim i as long
for i = 1 to Sheets.count
...do something to the sheet...
next i


I am happy to do Dim S as Worksheet: for Each S in sheets ...
[If sheets contains any charts, I do not understand the data.]

Thanks.



Glad to help. Thanks for the feedback.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default For Each Type mismatch

I am happy to do Dim S as Worksheet: for Each S in sheets ...

If you are only looking at worksheets, then technically it would be better
to iterate the Worksheets collection rather than Sheets collection (that
way, if a chart sheet was ever added, you code would not choke on it)...

Dim S As Worksheet
For Each S In Worksheets
...etc...

Rick Rothstein (MVP - Excel)



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 279
Default For Each Type mismatch

In message of Sun, 10 Apr 2011 12:45:46 in
microsoft.public.excel.programming, Rick Rothstein <rick.newsNO.SPAM@NO.
SPAMverizon.net writes
I am happy to do Dim S as Worksheet: for Each S in sheets ...


If you are only looking at worksheets, then technically it would be
better to iterate the Worksheets collection rather than Sheets
collection (that way, if a chart sheet was ever added, you code would
not choke on it)...

Dim S As Worksheet
For Each S In Worksheets
...etc...


I am now informed about the Worksheets and thank you.
OTOH, my data is not expected to contain charts. If a chart is given to
me, I would be happier with a noisy failure than a quiet "success".
--
Walter Briscoe
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default For Each Type mismatch

On Sun, 10 Apr 2011 22:58:16 +0100, Walter Briscoe wrote:

I am now informed about the Worksheets and thank you.
OTOH, my data is not expected to contain charts. If a chart is given to
me, I would be happier with a noisy failure than a quiet "success".
--
Walter Briscoe


If you want to test for that gracefully, instead of just having things fail, you could try:

============
Option Explicit
Sub foo()
Dim S As Object

For Each S In Sheets
If S.Type < xlWorksheet Then
MsgBox ("You've got a non-worksheet in your workbook")
Else
... your code ...
End If
Next S

End Sub
=======================

This might not work in versions prior to 2007. In addition, the enumeration codes listed seem to be improperly implemented. A Chart type of sheet returns a value of "3" for Type, but that value is supposed to be XlExcel4MacroSheet. xlChart should be -4109
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default For Each Type mismatch

On Apr 10, 2:48*am, Walter Briscoe
wrote:
I am using Microsoft Office Excel 2003(11.8328.8329) SP3.
I like to type variables as tightly as possible.

I am building some code which is going to loop through Sheets.
To that end, I start with "For Each S in Sheets
Next S"

I start by declaring S with "Dim S" as I do not know its type.

View/Locals Window shows the type of S as Variant/Object/Sheet1.
When I replace the declaration of S with Dim S as Sheet1,
Foe Each S in Sheets gets a "Run-time error '13':" which is a "Type
Mismatch". The "best" I can do is "Dim S as Object".

When I look at the "For Each...Next Statement" help, I read
"element Required. Variable used to iterate through the elements of the
collection or array. For collections, element can only be a Variant
variable, a generic object variable, or any specific object variable.
..."

What is the definition of "specific object variable" and why does "Dim S
as Sheet1" not produce one of the right type?
I hope someone will quote from as authoritative a URL as possible.
Thanks.
--
Walter Briscoe


dim sh as worksheet
for each sh in thisworkbook.sheets
ddd
next sh
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
Type mismatch: array or user-defined type expected Naftas Excel Programming 0 March 23rd 10 12:38 PM
Type Mismatch: array or user defined type expected ExcelMonkey Excel Programming 4 July 6th 06 03:40 PM
Type mismatch using rnge as Range with Type 8 Input Box STEVE BELL Excel Programming 11 December 3rd 05 05:02 AM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM


All times are GMT +1. The time now is 07:24 AM.

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"