Results 1 to 7 of 7

Thread: [Excel] Range of cells -> VBA Array

  1. #1

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    [Excel] Range of cells -> VBA Array

    is it possible?

    I know the reverse is (from and arry to a range of cells) but I just tried the reverse and it doesn't like it.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: [Excel] Range of cells -> VBA Array

    You can definitely do it cell by cell:
    Code:
    Sub Macro1()
    Dim junk As Variant
    Dim aRange As Range
    Dim aCell As Range
    
    junk = Array(0, 0, 0, 0, 0, 0, 0, 0)
    Debug.Print junk(0), junk(1), junk(2), junk(3), junk(4), junk(5), junk(6), junk(7)
    
    Set aRange = Range("D9:K9")
    i = 0
    For Each aCell In aRange
        junk(i) = aCell.Value
        i = i + 1
    Next
    Debug.Print junk(0), junk(1), junk(2), junk(3), junk(4), junk(5), junk(6), junk(7)
    
    End Sub
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [Excel] Range of cells -> VBA Array

    No point in putting it in the array then

    The idea is to transfer spreadsheets into Access from Excel.
    - Docmd.TransferSpreadsheet
    - open Excel, loop through cells
    - open excel and grab a range as an array

    I thought the last one would be faster than cell by cell, as it is when you put a VBA array into an excel range.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: [Excel] Range of cells -> VBA Array

    Yes it is possible....

    VB Code:
    1. Dim arrMyArray()
    2.   arrMyArray = Excl.Range("A1").CurrentRegion
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [Excel] Range of cells -> VBA Array

    Hmmm thanks for the reply. But not quite what I wanted (from reading the help files). The CurrentRegion method seems to give the whole range that is surrounded by empty cells. I want the selected range in an array (assuming the selected range is one row of data) without having to loop through all the cells.

    I can loop through them, but it will be slower than if they are 'magically' put into an array. I don't know if it is possible though as I suppose a range may be of different sized cells/groupings of cells.

    Thanks for the suggestion tho.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: [Excel] Range of cells -> VBA Array

    "ActiveSheet.Selection" is the Range of selected cells on the currently displayed sheet.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: [Excel] Range of cells -> VBA Array

    Vince..

    You can specify the range.. it was only an example..

    VB Code:
    1. Dim arrMyArray()
    2.   arrMyArray = Excl.Range("A1:G1")
    3.   Dim i As Integer
    4.   For i = LBound(arrMyArray, 2) To UBound(arrMyArray, 2)
    5.     MsgBox arrMyArray(1, i)
    6.   Next i

    Although even as this is one row it still populates a two dimensional array
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

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