Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA call function with variable name

Tags:

excel

vba

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
like image 730
BWG Avatar asked Sep 13 '11 18:09

BWG


1 Answers

You should write a function that accepts the CB value as a parameter and then uses a select case to call the appropriate formatting function.

Something similar to this

Function SelectFormatting(Name as String) As Boolean
Select Case CBinst.Value
Case "Text1":
   SelectFormatting = Text1FormattingFunction()
Case "Text2":
   .
   .
   .
End Select
End Function
like image 95
Pepe Avatar answered Oct 21 '22 17:10

Pepe