View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Option Base 1; how to also make auto-arrays set to base 1?

Just an additional piece of advice. NEVER declare an array with only the
upper bound. E.g.,

Dim V(10) As Long

This is dependent on the Option Base setting (which as you have found out
applies only to the module in which it occurs). In some circumstances, V
will have 10 elements, and in other circumstances it will have 11, depending
on Option Base. This will cause bugs if you copy code from one module to
another. Always declare your arrays with both the lower and upper bounds.

Dim V(1 To 10) As Long
' or
Dim V(0 To 9) As Long


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)





"Keith" wrote in message
...
Thank you also to Tom, Bob, and Chip for your feedback. Chip, I like the
idea of UBound and LBound in concept, I just haven't gone there in code
because it adds a layer of complexity that my brain hasn't rewired itself
for when writing (and re-reading, tweaking) code ;-)

Tom and Bob- I learned a valuable thing today- there was one small code
difference that Chip alluded to in his response, and that I wouldn't have
caught if both of you hadn't tested my code- I always thought that Option
Base 1 only had to be in one Module to work for the whole workbook, like a
public declaration. The code I was using was in a separate module and I
added the OB1 to the top just to provide copy/paste code in my post. I
just tested, and apparently it is a module-specific setting. Wow. I'm
surprised I haven't run into problems before, but that's probably because
I tend to declare arrays whenever possible, and always declare them with a
base of 1 in addition to having Option Base 1 in a module somewhere. Now I
know better.

Now that I know I'd have to have OB1 in every module (and every worksheet
with code) I'm much more inclined to stick with a zero-bound system and/or
the UBound/LBound approach!

Many, many thanks for the help to all of you!
Keith

"Bob Phillips" wrote in message
...
and me in XL2007

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my
addy)



"Tom Ogilvy" wrote in message
...
Your code returned a 1 for me using xl2003.

--
Regards,
Tom Ogilvy




"Keith" wrote:

Using XL 2003.
I generally set Option Base 1 in my projects, as a personal preference
(MS
gave us the option, so I'd think either way is ok).

However, even in Option Base 1 project, some arrays don't start with 1,
such
as:

Option Base 1
Sub testme
CArray = Array(1,2,3,4)
Msgbox CArray(1) 'returns a value of 2
End Sub

Is there a way to ensure that these 'on the fly' arrays also start with
Base
1, or am I stuck with having mixed array types if I assign these arrays
on
the fly?

Thanks!
Keith