具有变量名称的Excel VBA调用函数

时间:2011-09-13 18:16:25

标签: excel excel-vba vba

我尝试使用基于组合框值在运行时生成的变量名调用函数。这在大多数语言中都很简单,但我似乎无法在Excel VBA中找到它,我怀疑这是因为我不太了解编译器的工作原理。我发现有几个帖子很接近,但似乎并不是很有效。下面的代码是错误的,但应该知道我想要什么。

由于

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

4 个答案:

答案 0 :(得分:17)

CallByName是您完成任务所需要的。

例如: Sheet1中的代码

Option Explicit
Public Function Sum(ByVal x As Integer, ByVal y As Integer) As Long
    Sum = x + y
End Function

代码是Module1(bas模块)

Option Explicit

Sub testSum()
Dim methodToCall As String
methodToCall = "Sum"

MsgBox CallByName(Sheet1, methodToCall, VbMethod, 1, 2)
End Sub

运行方法testSum使用字符串变量中给出的方法名称调用方法Sum,传递2个参数(1和2)。调用函数的返回值作为CallByName的输出返回。

答案 1 :(得分:1)

以上内容可行,但不适用于大量名称

使用Application.Run(MacroName,Parameters)

你必须确定有一个宏,但它比上面更好,因为没有选择语句。

答案 2 :(得分:0)

您应该编写一个接受CB值作为参数的函数,然后使用select case来调用相应的格式化函数。

与此类似的东西

Function SelectFormatting(Name as String) As Boolean
Select Case CBinst.Value
Case "Text1":
   SelectFormatting = Text1FormattingFunction()
Case "Text2":
   .
   .
   .
End Select
End Function

答案 3 :(得分:0)

关于我上面的回答,您可能还会发现这对检查宏是否存在很有用

'=================================================================================
'- CHECK IF A MODULE & SUBROUTINE EXISTS
'- VBA constant : vbext_pk_Proc = All procedures other than property procedures.
'- An error is generated if the Module or Sub() does not exist - so we trap them.
'---------------------------------------------------------------------------------
'- VB Editor : Tools/References - add reference TO ......
'-    .... "Microsoft Visual Basic For Applications Extensibility"
'----------------------------------------------------------------------------------
'- Brian Baulsom October 2007
'==================================================================================
Sub MacroExists()
    Dim MyModule As Object
    Dim MyModuleName As String
    Dim MySub As String
    Dim MyLine As Long
    '---------------------------------------------------------------------------
    '- test data
    MyModuleName = "TestModule"
    MySub = "Number2"
    '----------------------------------------------------------------------------
    On Error Resume Next
    '- MODULE
    Set MyModule = ActiveWorkbook.VBProject.vbComponents(MyModuleName).CodeModule
    If Err.Number <> 0 Then
    MsgBox ("Module : " & MyModuleName & vbCr & "does not exist.")
    Exit Sub
    End If
    '-----------------------------------------------------------------------------
    '- SUBROUTINE
    '- find first line of subroutine (or error)
    MyLine = MyModule.ProcStartLine(MySub, vbext_pk_Proc)
    If Err.Number <> 0 Then
    MsgBox ("Module exists      : " & MyModuleName & vbCr _
           & "Sub " & MySub & "( )  : does not exist.")
    Else
    MsgBox ("Module : " & MyModuleName & vbCr _
        & "Subroutine   : " & MySub & vbCr _
        & "Line Number : " & MyLine)
    End If
End Sub
'-----------------------------------------------------------------------------------