Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default Option Explicit and Arrays

Just caught a typo.

In Excel 97 VBE, the following general module code doesn't generate a compile
error.

'---- begin VBA ----
Option Explicit

Sub foo()
Dim n As Long
n = 5
ReDim y(1 To 5) '<- !!
MsgBox n
End Sub
'---- end VBA ----

Is the ReDim line an array variable declaration similar to Dim, or a statement,
or a hybrid? Does this work the same in 2K and XP?

--
Never attach files.
Snip unnecessary quoted text.
Never multipost (though crossposting is usually OK).
Don't change subject lines because it corrupts Google newsgroup archives.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Option Explicit and Arrays

Harlan Grove wrote:
Just caught a typo.

In Excel 97 VBE, the following general module code doesn't generate a compile
error.

'---- begin VBA ----
Option Explicit

Sub foo()
Dim n As Long
n = 5
ReDim y(1 To 5) '<- !!
MsgBox n
End Sub
'---- end VBA ----

Is the ReDim line an array variable declaration similar to Dim, or a statement,
or a hybrid? Does this work the same in 2K and XP?

Works as is in 2002 (XP).

It causes an error if it is changed to:
ReDim Preserve y(1 To 5)

Apparently this is a feature - I found in the help the following:
"Caution: The ReDim statement acts as a declarative statement if the
variable it declares doesn't exist at module level or procedure level.
If another variable with the same name is created later, even in a
wider scope, ReDim will refer to the later variable and won't
necessarily cause a compilation error, even if Option Explicit is in
effect. To avoid such conflicts, ReDim should not be used as a
declarative statement, but simply for redimensioning arrays."


Regards,

Matthew

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Option Explicit and Arrays

Harlan,

The Help file indicates that this is the expected behavior.


Caution The ReDim statement acts as a declarative statement if
the variable it declares doesn't exist at module level or
procedure level.
<<<

It is the same in 2000 and 2002.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Harlan Grove" wrote in message
...
Just caught a typo.

In Excel 97 VBE, the following general module code doesn't

generate a compile
error.

'---- begin VBA ----
Option Explicit

Sub foo()
Dim n As Long
n = 5
ReDim y(1 To 5) '<- !!
MsgBox n
End Sub
'---- end VBA ----

Is the ReDim line an array variable declaration similar to Dim,

or a statement,
or a hybrid? Does this work the same in 2K and XP?

--
Never attach files.
Snip unnecessary quoted text.
Never multipost (though crossposting is usually OK).
Don't change subject lines because it corrupts Google newsgroup

archives.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Option Explicit and Arrays

There are two types of arrays(i.e. static and dynamic). A
static array is dim myarray(1 to 5). A dynamic array will
appear as dim myarray(). And then the code further down,
will show the redim statement specifying the parameters,
such as redim myarray (1 to foundfiles.count). The error
in your code below is that the redim refers to the wrong
letter (s/b n, not y) or that redim Y was not initalized
with dim y.

Robertw



-----Original Message-----
Just caught a typo.

In Excel 97 VBE, the following general module code

doesn't generate a compile
error.

'---- begin VBA ----
Option Explicit

Sub foo()
Dim n As Long
n = 5
ReDim y(1 To 5) '<- !!
MsgBox n
End Sub
'---- end VBA ----

Is the ReDim line an array variable declaration similar

to Dim, or a statement,
or a hybrid? Does this work the same in 2K and XP?

--
Never attach files.
Snip unnecessary quoted text.
Never multipost (though crossposting is usually OK).
Don't change subject lines because it corrupts Google

newsgroup archives.
.

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
Remove explicit formatting only Mitch Powell Excel Discussion (Misc queries) 6 September 9th 09 09:47 PM
conditionl format overrides explicit format John Keith Excel Discussion (Misc queries) 2 July 16th 08 03:16 PM
Option Explicit [email protected] Excel Discussion (Misc queries) 5 April 30th 08 11:19 PM
how to create a chart from the source which does not have explicit numbers [email protected] Charts and Charting in Excel 5 February 6th 07 08:01 PM
option explicit Dave F Excel Discussion (Misc queries) 2 September 5th 06 02:46 PM


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