VBA复选框附加代码

时间:2016-02-10 08:56:48

标签: excel vba excel-vba checkbox

如果可以将另一个事件添加到VBA Excel中的复选框,我就会徘徊。我创建了一个像这样的复选框:

ActiveSheet.CheckBoxes.Add(.Left, .Top,.Width, .Height).Select
With Selection
    .Caption = ""
    .LinkedCell = "AN2"
    .Value = xlOn
End With

我想知道是否可以添加代码,以便每当有人检查时,某个单元格会获得当前日期,如:

Cells(1,"A").Now()

干杯!

1 个答案:

答案 0 :(得分:0)

正如@Rory所说,answear是:

添加" .OnAction ="宏的名称"然后你可以编写一个对应于点击的宏:

With Selection
    .Caption = ""
    .LinkedCell = "AN2"
    .Value = xlOff
    .OnAction = "DateClicked"
End With

Function DateClicked()
    If Cells(2,"AN") = "TRUE" Then
        Cells(2,"A") = Now()
    Else 
        Cells(2,"A") = ""
    End If
End Function