将数组标注为动态大小时出错

时间:2017-03-21 22:29:55

标签: vba

以下代码:

Sub mySub()

    Dim s As String
    s = "jdsjakfjdaskl"
    Dim a(Len(s)) As String

End Sub

给出错误

  

"编译错误:需要常量表达式"。

我真的很感激有关如何解决这个问题的任何建议,谢谢!

1 个答案:

答案 0 :(得分:1)

如果希望在运行时设置数组的大小,则必须使用ReDim

Sub mySub()

    Dim s As String
    Dim a() As String
    s = "jdsjakfjdaskl"
    ReDim a(1 To Len(s)) As String
    'The "As String" in the above statement is optional as it already knows
    'it is of type String, so you can simplify that if you like to:
    'ReDim a(1 To Len(s))

End Sub