My Assistant
![]() ![]() |
|
|
Feb 23 2012, 01:14 PM
Post
#1
|
|
|
UtterAccess Guru Posts: 625 |
I'm having some problems when I try to handle an array from a range. Here's the code:
CODE Dim str_DATE_Ary As Variant Dim temp_date as String Dim counter as long range_string = "A2:A" & lr str_DATE_Ary = Range(range_string) for counter = 1 to lr temp_date = str_DATE_Ary(counter, 1) next This gives me a subscript out of range error. However, if I just use msgbox, it works fine. |
|
|
|
Feb 23 2012, 01:46 PM
Post
#2
|
|
|
UtterAccess VIP Posts: 9,266 From: Wisconsin |
kruuth,
Your For counter is going too high, since your array starts at Row 2. Let's say your variable "lr" is 5. Then the array is built from A2:A5, which is 4 values. Next, your counter is going from 1 to 5 to read from the array. When the loop hits 5, it goes beyond the range of your array. Your loop counter needs to go to "lr-1" if the start of the range is hard-coded to be 2, as your example shows. You haven't defined "lr" anywhere, let alone assigned a value to it, but I'm assuming it's a Long Integer datatype. It's worth mentioning that you're loading cell data into a Variant array, and then loading that Variant data into a String variable without any sort of conversion. Also, your String variable's name implies that it is holding some sort of Date, which raises the next question; is it a Date? Or a String? Or something else? By the way, rather than looping, like you posted, you could just use this to end up with the same value in temp_date: temp_date = str_DATE_Ary(lr-1, 1) Hope this helps, Dennis |
|
|
|
Feb 23 2012, 01:55 PM
Post
#3
|
|
|
UtterAccess VIP Posts: 4,295 |
There are only (lr-1) items in the array.
eg if lr is 21 then there are 20 items in the array |
|
|
|
Feb 23 2012, 02:09 PM
Post
#4
|
|
|
UtterAccess Guru Posts: 625 |
D'oh. It's the range <facepalm>
Actually I have LR defined I just left it out. |
|
|
|
Feb 23 2012, 02:14 PM
Post
#5
|
|
|
UtterAccess VIP Posts: 4,295 |
You could avoid this problem by using LBound and UBound.
CODE For I = LBound(str_DATE_Ary) To UBound(str_DATE_Ary)
|
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 19th May 2013 - 10:29 PM |