单击按钮时将对象作为参数传递

时间:2019-04-09 20:38:24

标签: excel vba

是否可以将对象作为参数发送给按钮单击的子例程?假设我有一个名为“清除表格”的按钮。我想将ActiveSheet对象发送给按钮单击分配给的子例程。

宏观名称:

TEST TEMPLATE.xlsm'!Sheet1.ClearSheet(ThisWorkbook.ActiveSheet)

子例程:

Private Sub ClearSheet(ByVal sh As Worksheet)

    Dim lastRow As Long
    lastRow = GetLastRow(sh, 1)
    If lastRow >= 12 Then sh.Range("A12:O" & lastRow).Clear

End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用辅助功能调用

Public sub CallClearSheet()
    call  ClearSheet(ActiveSheet)
end sub 


'use public and byref  in this function
Public Sub ClearSheet(Byref sh As Worksheet)

    Dim lastRow As Long
    lastRow = GetLastRow(sh, 1)
    If lastRow >= 12 Then sh.Range("A12:O" & lastRow).Clear

End Sub