I’m trying to call a function with a variable name that is generated at run time based upon a combo box value. This is straightforward in most languages but I can’t seem to figure it out in Excel VBA, I suspect this is because I don’t really understand how the compiler works. I’ve found several posts that are close but don’t quite seem to do the trick. The code below is wrong but should give an idea of what I want.
Thanks
Sub main()
'run formatting macros for each institution on format button click
Dim fn As String
Dim x As Boolean
'create format function name from CB value
fn = "format_" & CBinst.Value
'run function that returns bool
x = Eval(fn)
...
End Sub
CallByNameis what you’ll need to accomplish the task.example:
Code in Sheet1
Code is Module1 (bas module)
Running the method
testSumcalls the methodSumusing the name of the method given in a string variable, passing 2 parameters (1 and 2). The return value of the call to function is returned as output ofCallByName.