How to define the Array Size in variable

时间:2016-04-25 09:04:59

标签: vba excel-vba excel

my array size is decided by the some other function. but when i tried to define the size of the array using the variable i am getting the error "Constant Expression Required"

i would like define the size of the array

Option Explicit
Sub Abcd()
    Dim n_variables As Integer
    n_variables = def()
    MsgBox "n_variables" & n_variables
    Dim abc(1 To n_variables) As Integer  
End Sub
Function def()
    def = 100
End Function

is there any way i can define the array size in variable format? Any one help me please.

1 个答案:

答案 0 :(得分:2)

Dim n_variables As Integer
n_variables = def()
MsgBox "n_variables" & n_variables
Dim abc() As Integer  
Redim abc(1 To n_variables) As Integer