Results 1 to 6 of 6

Accessing elements within arrays

Threaded View

  1. #1
    Registered User
    Join Date
    11-10-2009
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Accessing elements within arrays

    Hi All

    I've been teaching myself VBA for the purpose of processing some time resolved data from an instrument. I've come up against a problem and I just can't work out why the code is failing. (using excel 2007, VBA 6.5)

    I declare some arrays as dynamic, then ReDim them to a user defined number (with Option Base 1) and input data from a spreadsheet into the array. I can then output the data into the spreadsheet again and the lbound and ubound functions return the correct values for the array. What I cannot do is actually access individual elements. If I specify array(i) I get the "Subscript is out of range" error.

    The code relevant to this problem is as below (simplified as there are more arrays):

    Dim an171rw() As Double
        Dim an171bc() As Double
        Dim i As Integer
        Dim y As Double                         'Intervening lines left out
    
        ReDim an171rw(1 To rowcount)         'rowcount is the user-defined variable
        ReDim an171bc(1 To rowcount)
    
        an171rw() = Range(Cells(AnStCell, col171), Cells(AnEdCell, col171)).Value
                                                      'assign cell values to array
    
        Worksheets("Calculations").Range(Cells(AnStCell, 16), Cells(AnEdCell,16)).Value = an171rw
                                                      'outputs array to worksheet
    
        an171bc = an171rw
    
        Worksheets("Calculations").Range(Cells(AnStCell, 10), Cells(AnEdCell,10)).Value = an171bc
                                                    'used to check array has values and will then be used to check operations worked 
            
            For i = LBound(an171bc) To UBound(an171bc)
                y = an171rw(i) * 2           
                                                           ' Error occurs at the above line, just an example of what I would like to do, will be extended
                an171bc(i) = y
                                                            ' and here if I comment out previous line
           Next i
    etc,etc

    (The arrays in this example have 854 lines. so 1-854 are the bounds)

    I have tried different methods for basically everything. Including ReDiming using only rowcount in brackets, not using y, using a For each loop. I tried just putting an integer in (e.g. an171rw(1)) and that has the same result.

    I'm sure it is something simple but I just cannot figure out the problem.

    Thanks for any help
    Last edited by JP789; 11-11-2009 at 09:04 PM. Reason: Apologies, didn't read the rules properly and missed the code declaration bit

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1