Excel/VB函数错误:“需要常量表达式" [英] Error in Excel/VB function: "Constant expression required"

查看:153
本文介绍了Excel/VB函数错误:“需要常量表达式"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VB的新手,我很难做一些应该很简单的事情.

I'm new at VB and I'm having a hard time doing something that should be very simple.

我正在尝试制作一个长度为n + 1的数组,并且在运行以下代码时不断显示需要常量表达式"

I'm trying to make an array of n+1 length and I keep getting "Constant expression required" when running the following code

Function binomial(n As Integer, p As Double)
Dim probabilities(0 To n) As Double
End Function

我了解到用于构建数组的参数必须是常量,但是我是否可以从函数的参数中创建一个?

I understand that the arguments used to build the array must be constants, but do I create one from the argument of the function?

提前谢谢

推荐答案

您不能针对可变大小进行DIM.改掉它

You can't DIM against a variable size. ReDim it instead

例如

Function binomial(n As Integer, p As Double)
Dim probabilities() As Double
ReDim probabilities(0 To n)
MsgBox LBound(probabilities)
MsgBox UBound(probabilities)
End Function

Sub test()
Call binomial(3, 2)
End Sub

运行子测试"

这篇关于Excel/VB函数错误:“需要常量表达式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆