如果单击单选按钮,我有一个运行函数的宏(清除每个命名范围取决于application.caller.name)
Sub Clear_Click()
Dim s, f, arr
s = ActiveSheet.Shapes(Application.Caller).Name
arr = Array("NamedArray1", "NamedArray2", "NamedArray3", "NamedArray4")
Select Case s
Case "Clear7"
For i = LBound(arr) To UBound(arr)
ThisWorkbook.Worksheets("info").Range(arr(i)).value = ""
Next i
Case Else
f = arr(Right(s, 1) - 1)
ThisWorkbook.Worksheets("info").Range(f).value = ""
End Select
End Sub
一切正常。
现在我需要从其他功能中单击Clear7单选按钮 所以如果我这样做
Sub test()
Application.Run ActiveSheet.Shapes("Clear7").OnAction
End Sub
我在s = ActiveSheet.Shapes(Application.Caller).Name
上收到错误,因为我认为没有Application.Caller。
那么如何从其他功能中单击单选按钮?
答案 0 :(得分:1)
如果你正在使用Application.Caller,但是你想在没有人需要点击按钮的情况下运行代码,那么这里就是你可以做到的。
注意:因为Clear_Click有一个参数,所以它不会出现在" assign macro"将它附加到按钮时列出,但您可以直接在框中键入其名称,这样可以正常工作。
Sub Clear_Click(Optional callerName As String = "")
Dim s, f, arr, cn As String, i
Dim sht As Worksheet
cn = IIf(Len(callerName) > 0, callerName, Application.Caller)
'Debug.Print cn
Set sht = ThisWorkbook.Worksheets("info")
arr = Array("NamedArray1", "NamedArray2", "NamedArray3", "NamedArray4")
Select Case cn
Case "Clear7"
For i = LBound(arr) To UBound(arr)
sht.Range(arr(i)).Value = ""
Next i
Case Else
f = arr(Right(s, 1) - 1)
sht.Range(f).Value = ""
End Select
End Sub
Sub test()
ClickIt "Clear7"
End Sub
'run a macro attached to a shape and pass its name as a parameter
Sub ClickIt(sName As String)
Application.Run ActiveSheet.Shapes(sName).OnAction, sName
End Sub