I am using a Function to make an array. The input is also an array. When running it gives me an invalid ReDim
compile error. Before this was run in a sub routine and the ReDim
worked well but now I changed it in a Function and it gives the invalid ReDim
compile 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
The ReDim statement is used to size or resize a dynamic array that has already been formally declared by using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts). Use the ReDim statement repeatedly to change the number of elements and dimensions in an array.
Redim Preserve allows you to change the dimensions of an array while keeping the contents of the array. The Redim Preserve at the end of each loop is adding another row to the array.
The ReDim statement can appear only in a procedure. Unlike the Dim statement, ReDim is executable, it forces the application to carry out an action at runtime. Dim statements aren´t Page 4 executable, and they can appear outside procedures.
Preserve is an optional keyword used to preserve the data in an existing array when you change the size of the last dimension. The ReDim statement is used to resize an array that has already been explicitly declared using the Dim, Private, or Public statement.
Welkom op Stack overflow.
As said you cannot redim the function itself. Therefore use a temporary variable and in the end transfer its content to your function:
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
bmhussel
is the name of your function and not the name of a variable. You cannot Redim
your function.
ReDim bmhussel(1 To rijaantal + 1, 1 To kolomaantal + 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With