Excel VBA中恒作为参数 [英] Excel VBA constant as argument

查看:229
本文介绍了Excel VBA中恒作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提前对不起,我是一个自学成才的程序员VBA,我不知道如何短语我的问题!
我已经声明它们具有类似名称的常量,即

Sorry in advance, I'm a self-taught VBA programmer, and I'm not sure how to phrase my question! I've declared constants which have similar names, i.e.

Public Const BP1Enable = "something1something:someotherthing1someotherthing"
public const BP2Enable = "something2something:someotherthing2someotherthing"

等。

我有这些常量的10。我有这些常量作为参数子:

I have 10 of these constants. I have a sub with these constants as arguments:

Sub FieldEnable (ByVal enableconst)

现在我想用我的柜台打电话给在循环中FieldEnable子:

Now I want to call the FieldEnable sub in a loop using i as counter:

For i = 1 To 10

 BPfieldname = "BP" & i & "Enable"
 FieldEnable enableconst:=BPfieldname
Next i

这是不行的,正在发生的事情是,价值分配到子FieldEnable到enableconst,是BP1Enable,而不是不断BP1Enable即价值的something1something:someotherthing1someotherthing。

This does not work, what is happening is that the "value" assigned to enableconst in the sub FieldEnable, is "BP1Enable" instead of the value of the constant BP1Enable namely "something1something:someotherthing1someotherthing".

如何调用子FieldEnable时所使用的变量BPfieldname?

How can I use the variable BPfieldname when calling the sub FieldEnable?

我希望这是有道理的。任何帮助AP preciated。

I hope this makes sense. Any help appreciated.

推荐答案

将您的变量到一个单一的阵列。

Transform your variables into a single Array.

请参阅此 http://msdn.microsoft.com/en-us/库/ wak0wfyt.aspx

编辑:作为@sina正确地指出的那样,VBA不允许恒定的阵列,

as @sina has correctly pointed out, VBA does not allow constant arrays,

于是转而尝试这种

Dim BPEnable = {
  "something1something:someotherthing1someotherthing",
  "something2something:someotherthing2someotherthing",
  ....
}

你应该试试这个

Dim BPEnable
BPEnable = Array( _
  "something1something:someotherthing1someotherthing", _
 "something2something:someotherthing2someotherthing", _
 "..."
)


For i = 0 To UBound(BPEnable)
 BPfieldname = BPEnable(i)
Next i

这篇关于Excel VBA中恒作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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