对于Application.Run方法,“指示功能在哪里的Range对象”是什么?

时间:2018-09-05 02:19:12

标签: excel vba excel-vba

Range 对象可以作为Application.Run Method (Excel)的参数是什么意思?

文档说,第一个参数是“要运行的宏。它可以是带有宏名称的字符串,指示功能位置的 Range 对象或已注册的注册ID。 DLL(XLL)函数。如果使用字符串,则将在活动工作表的上下文中评估该字符串。“

这是否意味着具有函数名称或代码的范围?我的测试均无效:

Public Function TestFunctionA()
    MsgBox "It works!"
End Function

Sub FirstTestOfRunFromRange()
'Function name in a cell
    Dim rngA As Range
    Set rngA = ActiveSheet.Range("A1")
    rngA = "TestFunctionA"
    Application.Run "TestFunctionA"
    Application.Run rngA
End Sub

Sub SecondTestOfRunFromRange()
'Function code in a cell
    Dim rngA As Range
    Set rngA = ActiveSheet.Range("A1")
    rngA = "Public Function TestFunctionB()" & _
        vbCrLf & "MsgBox ""It works!""" & _
        vbCrLf & "End Function"
    Application.Run rngA
End Sub

Sub ThirdTestOfRunFromRange()
'Function code as one line per cell
    Dim rngA As Range
    Set rngA = ActiveSheet.Range("A1")
    rngA.Offset(0, 0) = "Public Function TestFunctionB()"
    rngA.Offset(1, 0) = "MsgBox ""It works!"""
    rngA.Offset(2, 0) = "End Function"
    Set rngA = rngA.CurrentRegion
    Application.Run rngA
End Sub

Sub FourthTestOfRunFromRange()
'Function code as one line in one cell
    Dim rngA As Range
    Set rngA = ActiveSheet.Range("A1")
    rngA = "Public Function TestFunctionB(): MsgBox ""It works!"": End Function"
    Application.Run rngA
End Sub

Sub FifthTestOfRunFromRange()
'Code step in a cell
    Dim rngA As Range
    Set rngA = ActiveSheet.Range("A1")
    rngA = "MsgBox ""It works!"""
    Application.Run rngA
End Sub

1 个答案:

答案 0 :(得分:2)

Application.Run的范围参数选项涉及Excel 4 Macro,即Range设置为Macro工作表中宏的第一个单元格。

enter image description here