Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: Function array, ReDim gives invalid ReDim

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
like image 522
Amir Avatar asked Dec 24 '12 23:12

Amir


People also ask

How does ReDim work VBA?

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.

What does ReDim preserve mean in VBA?

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.

What is the difference between dim and ReDim in VB?

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.

What are the use of preserve and ReDim statements in arrays?

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.


2 Answers

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
like image 112
K_B Avatar answered Sep 21 '22 23:09

K_B


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)

like image 25
Sorceri Avatar answered Sep 21 '22 23:09

Sorceri