Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Values into an Array 2

Status
Not open for further replies.

MattGreer

Technical User
Feb 19, 2004
471
US
I'm smacking my forehead because this should be easy but I'm unsure about how to correctly use the RANGE object.

I have a column of values, always starting at cell "E8". The number of values in the column may change. Here's the code I have so far:

Code:
Option Explicit
Option Base 1

Private Sub ReadInArray()
Dim i, y, x As Long
Dim c As Range

x = 0
i = 1

'Count the rows in the array

x = Range("E" & Rows.Count).End(xlUp).Row - Range("E8").Row

ReDim aLevels(x)

For Each c In Range("E8", Range("E8").End(xlDown))
    aLevels(i) = Range("E" & 'And here I'm stuck
    i = i + 1
Next
End Sub

How do I specify to start at cell E8, stuff that value into the array, and then move down and repeat?

Thanks for your help!!

Thanks!!


Matt
 
I'm thinking the missing line is:

Code:
aLevels(i) = c.value


But I'm getting a 'subscript out of range' on aLevels(i).

/confused

Thanks!!


Matt
 
OK, seems like the value if i is going one more than it should... Hmmmm...

Thanks!!


Matt
 
Well, not sure why the row count calculation gives me one less, I figure it's probably something to do with being inclusive. So I just added a "+1" at the end of it and problem solved. Bleh.

Thanks!!


Matt
 
An easy way to get a range into an array:
Code:
    Dim bLevels() As Variant
    bLevels = Range("E8", Range("E8").End(xlDown)).Value

    ' you get an array, 2d, being bLevels(x,y),
    ' where x is number of rows in range, and y is number of columns, base of 1


Cheers, Glenn.

Beauty is in the eye of the beerholder.
 
Tony,

The array was not previously declared.


GlennUK you are a MASTER! Sweet! Thanks!!

Actually, I am working with a 2d array, aLevels(2,X). The values I'm working with are timed values, so the first column is the date/time and the 2nd column is the data I'm interested in. I need to sort and smooth the data but I need to maintain that original index.

So if the first column starts in "E7" and ends at the bottom of "E8", would the code read like this?

Code:
bLevels = Range("E7", Range("E8").End(xlDown)).Value

Thanks!!


Matt
 

Assuming columns E & F
Code:
    Dim bLevels, i, j
    bLevels = Range(Range("E7:F7"), Range("E7:F7").End(xlDown)).Value
    For i = LBound(bLevels, 1) To UBound(bLevels, 1)
        For j = LBound(bLevels, 2) To UBound(bLevels, 2)
            Debug.Print bLevels(i, j)
        Next
    Next

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks skip, always appreciated!

Thanks!!


Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top