Array() = range().value

后端 未结 3 911
旧时难觅i
旧时难觅i 2021-01-20 15:27

I saw array() = range().value in an example and I\'m trying to understand how it works.

Sub test()
Dim arr() As Variant

arr() = Range(\"E5:E7\         


        
3条回答
  •  执笔经年
    2021-01-20 16:07

    This is a good read for you: http://www.cpearson.com/excel/ArraysAndRanges.aspx

    The reason you're getting "out of range" is because it returns a 2 dimensional array.

    Your line of code For i = 0 To UBound(arr) should be For i = 1 To UBound(arr,1)

    Also, the array starts at 1, so don't use the 0 For i = 1 to UBound(arr, 1)

    Your corrected code would be:

    Sub Test()
    
    Dim arr() as Variant
    arr = Range("E5:E7")
    For i = 1 To UBound(arr, 1)
        MsgBox (arr(i, 1))
    Next i
    
    End Sub
    

提交回复
热议问题