Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define the Array Size in variable

Tags:

excel

vba

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.

like image 391
surendra Avatar asked Oct 26 '25 12:10

surendra


1 Answers

Use the Redim statement:

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
like image 97
Excel Developers Avatar answered Oct 28 '25 05:10

Excel Developers