New VBA user here, engineer-not a programmer. Your patience is appreciated. Did spend hours in help and online trying to find this answer. Found some answers to this, but they were for php. Working with MS Office Home and Student 2007.
I have a list of 8 equations. I have the user input the number of the equation they want to evaluate, I call that number index. Is there a way to put the index into a function name when it's defined? such as:
Function PlotFun[index]= some f(x)
Or is it possible to redefine the function using an index within a subroutine? such as:
If index=1 then PlotFun=PlotFun[index] 'so PlotFun would equal PlotFun1
I think I read in the help that a function has to be defined in a function routine, so this one probably isn't feasible.
Or is it possible to read the function from a cell in excel? I tried using
Function PlotFun=Worksheets(1).Range("BC6")
where BC6 contains "24 - 50 * x + 35 * x ^ 2 - 10 * x ^ 3 + x * 4" which is correct syntax for a function of x, as it was copied from my PlotFun1 routine. But I don't have the syntax down right. I get Compile Error: Expected: End of statement, right at the equals sign.
I've also been referred to the lamda function, but again, I get the Expected: End of Statement error.
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
I need to use PlotFun or PlotFun1 in a few places within many subroutines. I don't want to have to write/copy code for each f(x). Perhaps there is a better way to do this than what I've thought of? I cannot use an array, as the equations are not all polynomials.
Thank you!