函数名称由VBA中的variables定义

新的VBA用户在这里,工程师 – 不是程序员。 你的耐心感激。 花了几个小时的帮助和在线试图find这个答案。 find了一些答案,但他们是为PHP。 使用MS Office家庭和学生2007年。

我有一个8个方程的列表。 我有用户input他们想评估的方程的数量,我称之为数字索引 。 有没有办法将索引放入一个函数名称时定义? 如:

Function PlotFun[index]= some f(x) 

或者是否有可能使用子程序中的索引重新定义函数? 如:

 If index=1 then PlotFun=PlotFun[index] 'so PlotFun would equal PlotFun1 

我想我在帮助中读到一个函数必须在函数例程中定义,所以这个可能是不可行的。

或者是有可能从Excel中的单元格读取function? 我试过使用

 Function PlotFun=Worksheets(1).Range("BC6") 

其中BC6包含“24 – 50 * x + 35 * x ^ 2 – 10 * x ^ 3 + x * 4”,这是正确的函数语法,因为它是从我的PlotFun1例程中复制的。 但是我没有正确的语法。 我得到编译错误:预期:语句结束,在等号。

我也被称为lamda函数,但是我又得到了Expected:End of Statement错误。

 Sub Bisection() Dim index Call FuncIndex 'sets the index number for which equation to use for analysis If index = 1 Then Dim PlotFun=Function(x) x ^ 3 - 6 * x ^ 2 + 11 * x - 6 If index = 2 Then Dim PlotFun=Function(x) 24 - 50 * x + 35 * x ^ 2 - 10 * x ^ 3 + x * 4 Do 'my calculations using PlotFun Loop 'for specified error or iterations End Sub 

我需要在很多子例程中的一些地方使用PlotFunPlotFun1 。 我不想为每个f(x)写/复制代码。 也许有更好的办法做到这一点比我想到的? 我不能使用数组,因为方程不是全部的多项式。

谢谢!

有没有办法将索引放入一个函数名称时定义? 如:

不是真的在线。 您可以使用select case switch或一系列if / else语句。 我通常更喜欢select大小写的方法。

注:这段代码没有为x指定任何值,所以它将被解释为一个Empty值或零值,除非你改变了代码或者提供了一个用户inputvariablesx值的方法。

 Sub WhichFunction() Dim i as Long Dim functionResult as Double i = Application.InputBox "Please enter the function's index #" Select Case i Case 1 functionResult = x ^ 3 - 6 * x ^ 2 + 11 * x - 6 Case 2 functionResult = - 50 * x + 35 * x ^ 2 - 10 * x ^ 3 + x * 4 Case 3 'etc. Case 4 Case 5 Case 6 Case 7 Case 8 Case else MsgBox "invalid function index!" End Select MsgBox functionResult End Sub 

关于你的代码:

我得到预期:声明结束错误

可能是因为你的Dim语句没有结束。 在VBA中,你通常不能声明和同时分配一个variables。 有例外和视觉捷径,但是这里没有必要讨论。

或者是有可能从Excel中的单元格读取function? 我尝试使用Function PlotFun=Worksheets(1).Range("BC6")

是的,一个函数可以从一个特定的单元格中读取,而不是你正在执行的方式。 你需要回到基础知识,学习如何创build一个function,例如:

 Function MyFunctionName(arg1 as integer, arg2 as integer) 'Code the evaluates some methods on the arguments provided Dim myVal as Double myVal = arg1 ^ arg2 MyFunction = myVal 'Return the calculated value End Function 

同样,你可以这样做:

 Function GetValueFromSheet(byVal cl as Range) 'This function will ALWAYS return the value in B6 GetValueFromSheet = Range("B6").Value End Function 

这听起来像你想改变使用什么algorithm,或者在你的情况下,基于你将传递给单个函数的参数,调用什么方程式。 这个问题可以通过使用众所周知的称为“ 策略模式 ”的软件模式来解决。 这个模式允许你改变一个algorithm,同时保持它的实现。

您可以将战略模式与工厂模式结合起来,以实现您正在寻求的所需架构。

这是一个相当明确的VBA实施的战略模式

希望这可以帮助。

VBA是尝试这样做的最糟糕的语言(实际上,这是尝试去做大多数事情的最糟糕的语言)。

不幸的是VBA函数不是值或对象,它们不能被存储在variables中。 因此,这些都不是远程合法的:

 Function PlotFun[index]= some f(x) Function PlotFun=Worksheets(1).Range("BC6") Dim PlotFun=Function(x) x ^ 3 - 6 * x ^ 2 + 11 * x - 6 

既然你不需要太多的function,最简单的解决scheme是这样的:

 Function PlotFun(index, x) Select Case index Case 1 PlotFun = x ^ 3 - 6 * x ^ 2 + 11 * x - 6 Case 2 PlotFun = 24 - 50 * x + 35 * x ^ 2 - 10 * x ^ 3 + x * 4 Case 3 ' ... and so on End Select End Function 

也就是说,将PlotFun声明为普通函数,并使用您的indexvariables来告诉它要评估哪个内部expression式。 缺点是你将不得不在任何你想要调用PlotFun的地方包含函数indexvariables。

请注意,此处的PlotFun = ...语法不会分配函数本身,而是VBA从函数的特定调用中设置返回值的方式。 在函数中, PlotFun就是要分配给variables的名称。