2

I need to create an array from a list of data in excel. The data will be of different lengths so i will have to find the end of it then create the array. I then need to loop through the array and use each value. I have not used VBA before so anything is helpful. This is what i have been able to get so far:

Sub Calc()
Dim endCell As String      
endCell = Range("B13").End(xlDown).Row    
endCell = "B13:B" + endCell    
Dim nums As Variant

nums = Range(endCell)

End Sub
1
  • 1
    to concatenate strings use '&' instead of '+'. So endCell = "B13:B" & endCell Commented Mar 26, 2012 at 17:26

4 Answers 4

4

You won't have to do that. Just do this:

Dim varValues() as Variant 

' ...

varValues = Range(endCell).Value

For a range object that comprises more than one cell, the value property will return an array of values.

If you don't know whether the range has more than one cell, but you want to have an array of values regardless, you could write a function to achieve that (thanks to brettdj for the inspiration):

Function GetValue(rng As Range) As Variant()
    Dim varResult() As Variant

    If rng.Cells.Count = 1 Then
        ReDim varResult(0 To 0) 'I prefer to be explicit about the base
        varResult(0) = rng.Value
    Else
        varResult = rng.Value
    End If

    GetValue = varResult
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

+1 but worth testing is endCell is more than 1 cell before varValues = Range(endCell).Value. Esle an error results.
1

use the Transpose function:

nums = Application.WorksheetFunction.Transpose(Range(endCell).value)

6 Comments

nums = Range("A1:B5") will return a 2D array starting from nums(1,1) (=A1) to nums(5,2) (=B5)...
Not using Transpose will result in a 3D Array (which is not usefull when working with a single column). I recommend the Transpose method.
uh... no. a = Range("A1:B5") returns a Variant/Variant(1 to 5, 1 to 2) and b = Application.WorksheetFunction.Transpose(Range("A1:B5").Value) returns a Variant/Variant(1 to 2, 1 to 5), which is the same as a, but transposed. More or less what you would expect but it does not change the number of dimensions.
According to he needs, he wants the value of a Single Column. Test it for a single column Range.
Ok I understand what you mean - Transpose returns a 1D array if the range has only one column, whereas the a = range method always returns a 2D array. Nice tip.
|
0

Starting from your code, you could loop over the array in the following way:

Sub Calc()
    Dim endCell As String

    endCell = Range("B13").End(xlDown).Row
    endCell = "B13:B" & endCell

    Dim nums As Variant
    Dim i As Long

    nums = Range(endCell)

    For i = LBound(nums,1) To UBound(nums,1)
        'do something with nums(i,1)
    Next i
End Sub

Comments

0

I am not sure if this is any better or cleaner.

Sub Calc()
Dim endCell As String

endCell = Range("B13").End(xlDown).Row
endCell = "B13:B" & endCell
Range(endCell).select

For each c in selection
   use any attribute of the cells one at a time like, c.value c.formula.....
Next c
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.