redim passed array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

is it possible to redim an array in a subroutine?
i get a 'subscript out of range' error or a 'temporarily locked' error
can anyone help

Thankfully, YisMan
 
Yes, you've just got to first declare the array as dynamic by leaving the
brackets empty:

Dim myArray() As String 'leave empty brackets for dynamic array
....
ReDim myArray(4) 'resizes the array to five elements
....
ReDim Preserve myArray(9) 'to increase, use Preserve to stop
reinitialisation of all elements
 
Read carefully !
On the body of sub/function is not allowed to redim parameters passed !
Solutions:
-sub/function can return array
-global array


"Martin" a scris:
 
thanks a million
how do i return an array with a function

function ReturnArray(....) as string?

and how do i call this function
newArray()=returnArray..?
 
thanks a million
how do i return an array with a function

function ReturnArray(....) as string?

and how do i call this function
newArray()=returnArray..?

This seems to work:

Sub TestReturnArray()
Dim S() As String

S = ReturnArray(5)
Debug.Print S(0)
Debug.Print S(4)
End Sub

Function ReturnArray(Elements As Long) As String()
Dim arTemp() As String
Dim j As Long

ReDim arTemp(Elements - 1)
For j = 0 To Elements - 1
arTemp(j) = "Hello World " & CStr(j)
Next
ReturnArray = arTemp()
End Function
 
Back
Top