VBA:函数数组,ReDim 给出无效的 ReDim

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14026346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:57:24  来源:igfitidea点击:

VBA: Function array, ReDim gives invalid ReDim

arraysfunctionvbaexcel-vbasubroutine

提问by Amir

I am using a Function to make an array. The input is also an array. When running it gives me an invalid ReDimcompile error. Before this was run in a sub routine and the ReDimworked well but now I changed it in a Function and it gives the invalid ReDimcompile error. What am I missing here?

我正在使用一个函数来创建一个数组。输入也是一个数组。运行时它给了我一个invalid ReDim编译错误。在此之前在子例程中运行并且ReDim运行良好但现在我在函数中更改了它并且它给出了invalid ReDim编译错误。我在这里缺少什么?

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

回答by K_B

Welkom op Stack overflow.

Welkom op 堆栈溢出。

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

回答by Sorceri

bmhusselis the name of your function and not the name of a variable. You cannot Redimyour function.

bmhussel是函数名而不是变量名。你不能Redim你的功能

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

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