|
-
Jun 9th, 2010, 04:46 AM
#1
Thread Starter
New Member
[RESOLVED] RangeToArray doesn't transfer all data?
Hi All,
I have a table of data with 13 rows and 6 columns. I want to transfer the data from columns 1, 3 and 5 into an array for processing.
I have used the following script:
arrValue = Empty
arrValue = Worksheets(1).Range("B3:B15,D3 15,F3:F15").Value
RangeToArray = True
I was hoping this would result in an array arrValue with dimensions (13,3) but when I try to access any element in column 2 or 3 I get a subscript out of range error. I thought that maybe the range was being copied as a single column but when I checked rown 14, column 1 I also got an out of range error.
Any ideas where I'm going wrong?
Thanks
Matt
-
Jun 9th, 2010, 05:38 AM
#2
Re: RangeToArray doesn't transfer all data?
i think you can only assign a contiguous range to an array
arrvalue, in your example, is dimensioned
arrvalue(1 to 13, 1 to 1)
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 9th, 2010, 05:55 AM
#3
Thread Starter
New Member
Re: RangeToArray doesn't transfer all data?
Thanks for your speedy reply. That would certainly explain it. If this is the case, is it possible to concatenate 2 1-D arrays to form 1 2-D array?
Thanks
Matt
-
Jun 9th, 2010, 06:15 AM
#4
Re: RangeToArray doesn't transfer all data?
yes, but any array assigned from a range is always 2d, even when only one column or row
it may be possible using copymemory, otherwise loop through the array
vb Code:
Dim arr() arr = Range("B3:B15") ReDim Preserve arr(1 To 13, 1 To 2) For i = 1 To 13 arr(i, 2) = Cells(i + 2, 4) Next
to work dynamically use ubounds of the array
you can only redim preserve the last dimension of a multi dimensional array
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 9th, 2010, 06:27 AM
#5
Thread Starter
New Member
Re: RangeToArray doesn't transfer all data?
That's great. Works perfectly. Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|