Results 1 to 3 of 3

Thread: Excel cell values into an array

  1. #1

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358

    Excel cell values into an array

    Guys

    I have a spreadsheet with a column of data (dates, to be exact) that could potentially change. I have some code which does what I want to do very well except the current values in the column are hard-coded into an array, thus:

    VB Code:
    1. myArray = Array("01/10/2003", "01/11/2003", "01/12/2003", "01/01/2004", "01/02/2004", "01/03/2004", "01/04/2004", "01/05/2004", "01/06/2004", "01/07/2004", "01/08/2004", "01/09/2004", "01/10/2004", "01/11/2004", "01/12/2004", "01/01/2005")

    As these dates may change or indeed be added to, I want to be able to dynamically build the array. I've tried the following, to no avail:

    VB Code:
    1. Do Until x = intRows
    2.    
    3.     ActiveSheet.Cells(x, 7).Activate
    4.    
    5.    
    6.         If strarray = "" Then
    7.        
    8.             strarray = ActiveSheet.Cells(x, 7) & Chr(34)
    9.    
    10.         Else
    11.             strarray = strarray & "," & Chr(34) & ActiveSheet.Cells(x, 7) & Chr(34)
    12.                    
    13.         End If
    14.        
    15.         x = x + 1
    16.     Loop
    17.  '   MsgBox strarray
    18.  
    19.     intlength = Len(strarray) - 1
    20.     strarray = Left(strarray, intlength)
    21.     myArray = Array(strarray)

    The problem seems to be that although strarray builds up and contains all the correct data, myArray is never populated. I'm pretty sure I'm doing something stupid but I'm just not sure what.

    Cheers
    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    you can't just say "make an array out of a string" and expect it to convert it how you want it (it will just make an array with one element).

    Fortunately there is a nice function called Split, which does the conversion for you. This is what you need in your current code:

    myArray = Split(strarray, chr(34))


    There are also two alternatives - the first is to put individual items into the array (rather than into the string and then converting to an array). Lke this:
    VB Code:
    1. Dim myArray
    2.     Redim myArray (intRows)
    3.  
    4.     Do Until x = intRows
    5.       myArray(x) = ActiveSheet.Cells(x, 7)
    6.       x = x + 1
    7.     Loop


    Also, you can get the full array in one go, without having to loop.
    VB Code:
    1. myArray = ActiveSheet.Range("G1:G" & intRows).FormulaArray
    I'm not sure .FormulaArray is right, it might need to be .Value , or .Text , or be left out completely.

  3. #3

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358
    Cheers Si, I'll give it a go.

    Tar lar.
    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

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