在Excel VBA中手动设置Application.Caller

时间:2016-07-21 22:58:15

标签: excel vba excel-vba

我一直在尝试使用button1来激活button2。问题是button2必须使用application.caller,默认为按下原始按钮(button1)。

如何为application.caller设置新值?

以下是示例代码:

Button1的:

Sub ShowAttack()
'Change the color of the other buttons to the background color.

'Pressed button to lighter color.
ActiveSheet.Shapes("AttackButton").Fill.ForeColor.RGB = 14277081

'Other buttons to darker colors.
ActiveSheet.Shapes("DefenseButton").Fill.ForeColor.RGB = 12566463
ActiveSheet.Shapes("SpellButton").Fill.ForeColor.RGB = 12566463

'Toggle Appropriate Cells to unhide
Dim button As Shape
Set button = ActiveSheet.Shapes("AttackButton")
rw = button.TopLeftCell.Row
Rows((rw + 4) & ":" & (rw + 74)).EntireRow.Hidden = False

'Hide appropriate cells within shown section

Set Application.Caller = ActiveSheet.Buttons("WeaponDetailsButton1").Name
Application.Run ActiveSheet.Buttons("WeaponDetailsButton1").OnAction

Range("AT51").Select

End Sub

按钮2:

Sub HideWeaponDetails()
    Dim b As Object, cs As Integer, rw As Integer
    Dim sel01 As Integer, sel02 As Integer
    Set b = ActiveSheet.Buttons(Application.Caller)
    With b.TopLeftCell
        cs = .Column
        rw = .Row
    End With

sel01 = rw + 4
sel02 = rw + 14

Rows(sel01 & ":" & sel02).Select
If Selection.EntireRow.Hidden = False Then
    Selection.EntireRow.Hidden = True
Else
    Selection.EntireRow.Hidden = False
End If

Range("A" & rw - 4).Select
    ActiveSheet.Buttons(Application.Caller).Name = "WeaponDetailsButton" & Selection(1).Value
Range("AV47").Value = ActiveSheet.Buttons(Application.Caller).Name

Range("A1").ClearOutline

End Sub

1 个答案:

答案 0 :(得分:4)

这样的事情:

Sub ShowAttack()

    '...do stufff

    HideWeaponDetails "WeaponDetailsButton1"

End Sub

Sub button2_click()
    'all this Sub does is call HideWeaponDetails
    HideWeaponDetails Application.Caller
End Sub


Sub HideWeaponDetails(TheCaller)
    'use TheCaller instead of Application.Caller
End Sub

...或将按钮本身作为参数传递