View Single Post
  #6   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?

Keith,

You should always make your code agnostic to the Option Base setting. ALWAYS
use LBound and UBound to get the bounds of the array. Relying on the Option
Base statement is an invitation to bugs when copy/pasting code between
modules and projects that may have no Option Base statement or an Option
Base statement different from that of the source module.

Dim Arr As Variant
Dim N As Long
Arr = Array("A", "B", "C", "D")
Debug.Print "LBound: " & CStr(LBound(Arr)), _
"UBound: " & CStr(UBound(Arr)), "Arr(1): " & CStr(Arr(1))
Debug.Print "First element: " & Arr(LBound(Arr))
Debug.Print "Last element: " & Arr(UBound(Arr))
'''''''''''''''''''''''''''''''
' List all elements
'''''''''''''''''''''''''''''''
For N = LBound(Arr) To UBound(Arr)
Debug.Print CStr(N), CStr(Arr(N))
Next N


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



"Keith" wrote in message
...
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