Arrays
I agree with you. I always use Option Base 1 because looping from 1 to
nItems is always more intuitive. In fact, I wish Base 1 were the default.
It seems to make more sense.
Likewise regarding error checking: After 23 years of trapping errors,
passing error flags, writing handler code, etc., I finally burned out. When
I think of the hundreds of lines of code I've written that essentially never
get executed it astounds me.
I've spent the last 5 or 6 years simply letting the errors appear and doing
a better job of debugging. Now I mostly use On Error Resume Next. I get
more done in less time.
My functions (mostly string and string arrays) return empty strings or empty
arrays for errors. For arrays I needed to write a UBnd function because
UBound will fail if an array is empty (undimensioned).
Option Base 1
Dim MyArray() As String
nItems = UBnd(MyArray) 'returns zero - it also accepts the Optional
Dimension parameter same as UBound
This has been VERY useful.
"Douglas Klimesh" wrote:
To start with you you are not declaring CloseChanges() as Date. Use instead:
Dim CloseChanges() As Date, ThisDate As Date, ThisClose As Double
Also, note that by default arrays are dim'ed starting with 0. So if you
ReDim CloseChanges(10) the array is actually CloseChanges(0) ..to..
CloseChanges(9). Use the directive Option Base 1 at the very beginning
of your module code to have an array that goes from 1 to 10 instead of 0
to 9. Or you could: ReDim CloseChanges(1 To YearsTotal)
I always use Option Base 1 because if there is an problem with my index
variable its value will most often be zero, which will give me a VBA
error and more obviously alert me to my programming problem.
David wrote:
hi Group,
I am have a hard time with the syntax for setting up an Array.
This is what I have so far:
Dim CloseChanges(), ThisDate As Date, ThisClose As Double
ReDim CloseChanges(YearsTotal)
For y = 1 To YearsTotal
ThisDate = ActiveCell.Value
ThisClose = ActiveCell.Offset(0, 6).Value
ActiveCell.Offset(-52, 0).Select
Next y
It did not like that I was using a variable to start with, so I went to the
ReDim syntax, but it still does not like it. The Array has only 2 data points
it is trying to pull in.
After it runs, I went to the Immediate Window and tried this:
?ThisClose(1)
But I get a message "Expect Array". Not sure wherre I am going wrong?
Thanks,
David
|