VBA: array of functions, ReDim gives invalid ReDim

I use a function to create an array. Input is also an array. On startup, it gives me an invalid ReDim compilation error. Before it was run in the subroutine, and ReDim worked fine, but now I changed it in the function and it gives an invalid ReDim compilation error. What am I missing here?

Thanks in advance! Amir

 Public Function bmhussel(filemx As Variant) rijaantal = UBound(filemx, 1) kolomaantal = UBound(filemx, 2) ReDim bmhussel(1 To rijaantal + 1, 1 To kolomaantal + 1) For i = 1 To rijaantal bmhussel(i, 1) = filemx(i, 1) bmhussel(i, 2) = filemx(i, 3) bmhussel(i, 3) = filemx(i, 5) bmhussel(i, 4) = filemx(i, 28) bmhussel(i, 5) = bucket(filemx(i, 28)) 'buckets maken next i End Function 
+4
source share
2 answers

Welkom op Stack overflow.

As said, you cannot redo the function yourself. So use a temporary variable and pass its contents to your function at the end:

 Public Function bmhussel(filemx As Variant) as Variant Dim rijaantal As Long Dim kolomaantal As Long Dim tmpArray as Variant rijaantal = UBound(filemx, 1) kolomaantal = UBound(filemx, 2) ReDim tmpArray (1 To rijaantal + 1, 1 To kolomaantal + 1) For i = 1 To rijaantal tmpArray(i, 1) = filemx(i, 1) tmpArray(i, 2) = filemx(i, 3) tmpArray(i, 3) = filemx(i, 5) tmpArray(i, 4) = filemx(i, 28) tmpArray(i, 5) = bucket(filemx(i, 28)) 'buckets maken next i bmhussel = tmpArray End Function 
+9
source

bmhussel is the name of your function, not the name of a variable. You cannot Redim perform its function.

ReDim bmhussel(1 To rijaantal + 1, 1 To kolomaantal + 1)

+4
source

All Articles