excel - 是否可以使用字符串动态调用subs

标签 excel vba

我尝试过类似的方法:

sub tester()
   'do something
End Sub

activesheet.cells(1,1) = 'tester'

sub foo()
     dim x as string
     x = activesheet.cells(1,1).value
     Call x

End Sub

目的是操纵一个单元格,以便当单元格值发生变化时,调用的子函数也会发生变化。

最佳答案

使用Application.Run

  • 这里有一个错误处理示例,值得花点时间。
Option Explicit

Sub Test1()
    MsgBox "First"
End Sub

Sub Test2()
    MsgBox "Second"
End Sub

Sub Foo()
    On Error GoTo ClearError
    
    Dim S As String: S = CStr(Range("A1").Value)
    Application.Run S

ProcExit:
    Exit Sub
ClearError:
    MsgBox "Run-time error '" & Err.Number & ":" & vbLf & Err.Description
    Resume ProcExit
End Sub

关于excel - 是否可以使用字符串动态调用subs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71332847/

相关文章:

vba - 复制两个 Excel 实例之间的范围

vba - 嵌套 If 语句有问题

excel - 用于查找包含前面带有 = 号的常量的单元格的代码

VBA比较2个数组循环>>突出显示差异并将其复制到第三列

vba - 评估 Excel VBA bool 条件(不是公式)

excel - Excel有语法高亮器吗

excel - 循环遍历日期以复制特定列

vba - Excel VBA : auto click and open file from website

excel - 使用 .pdf 打印而不提示对话框

vba - 删除 VBA 项目引用