将字符串范围存储在数组(vba)中

时间:2018-07-30 14:14:17

标签: string vba range

目标是创建一个数组,其中的项目在声明的范围内。很简单,但我无法正常工作...

'Declaration
Dim Measures As Variant
Dim MeasureRows As Long

'Search and store
Sheets(4).Activate
MeasureRows = Range("A" & Rows.Count).End(xlUp).row
Measures = Range(Cells(2, 1), Cells(MeasureRows, 1)).Value2 

MeasureRows可以很好地填充,但是当我检查“ Measures”时,它为空。例如

?MeasureRows
 780 
?Measures(MeasureRows) 'Returns "Subscript out of range"    
?Measures(1) 'Returns "Subscript out of range"

在谈到VBA时,我非常环保。所以请原谅一个基本问题。我尝试在网上搜索答案,但无法正常工作。我想我只是不知道搜索词的正确行话。

1 个答案:

答案 0 :(得分:2)

从图纸上读取时是二维的

Option Explicit
Public Sub test()
    Dim Measures As Variant
    Dim MeasureRows As Long

    With Worksheets("Sheet4")
        MeasureRows = .Cells(.Rows.Count, "A").End(xlUp).Row
        Measures = .Range(.Cells(2, 1), .Cells(MeasureRows, 1)).Value2
    End With

    Dim i As Long
    For i = LBound(Measures, 1) To UBound(Measures, 1)
        Debug.Print Measures(i, 1)
    Next
    Stop

End Sub