Alex Rivera | Logout

Excel VBA call function with variable name

Asked 2011-09-13T18:16:25.197
13

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
Edit
Report

1 Answer

1

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
answered 2011-09-13T18:22:46.277

Your Answer