0

这适用于 MsgBox,但当我取消注释赋值语句时,我收到类型不匹配错误。我有一个从 D1 开始的未知长度的字符串,我想存储在数组 MyArr 中。

Dim MyArr As Variant   
Range("D1").Select
I = 1

While ActiveCell <> Empty
    MsgBox ("this is in the active cell:" & ActiveCell.Value)
'   MyArr(I) = ActiveCell.Value
    I = I + 1
    ActiveCell.Offset(1, 0).Select
Wend
4

1 回答 1

1

MyArr(I)将失败,因为 MyArr 尚未定义为数组。查看您的代码,您似乎希望 MyArr 包含从 D1 到第一个空单元格的所有字符串

Dim MyArr
MyArr=Range("D1", Range("D1").End(xlDown))
If VarType(MyArr)>vbArray then 'more than 1 cell returned
    'D1 is in MyArr(1,1)
    'D2 is in MyArr(2,1)
    '...
    'Lastcell is in MyArr(Ubound(MyArr),1)
Else 'Only one cell found with text
    'D1 is in MyArr 
    'note no () -> one cell = no array
End If
于 2013-05-30T13:35:44.243 回答