Results 1 to 5 of 5

Thread: [RESOLVED] RangeToArray doesn't transfer all data?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    3

    Resolved [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,D315,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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    3

    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

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. Dim arr()
    2. arr = Range("B3:B15")
    3. ReDim Preserve arr(1 To 13, 1 To 2)
    4. For i = 1 To 13
    5.     arr(i, 2) = Cells(i + 2, 4)
    6. 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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    3

    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
  •  



Click Here to Expand Forum to Full Width