【发布时间】:2022-06-11 18:44:49
【问题描述】:
我有一个电子表格,其中我调整了两段 VBA 代码来执行两个不同的双击事件操作。
第一段代码在双击时在特定范围的单元格中输入“✓”,再次双击时将其删除:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("H2:H600,M2:V600")) Is Nothing Then
Application.EnableEvents = False
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
End If
Application.EnableEvents = True
End Sub
第二段代码在双击时在一系列单元格中输入日期/时间戳:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Coded by SunnyKow - 16/09/2016
Application.EnableEvents = False
On Error GoTo ErrorRoutine
'You can change the range here
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
'Update only if cell is empty
If Target = "" Then
Target = Now
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub
因为在单个工作表中不能有两个双击事件(作为单独的 VBA 代码),我如何合并这两个 VBA 片段,使其成为具有基于所选单元格范围的两个不同操作的单个代码片段。希望能提供任何帮助来解决此问题。
【问题讨论】:
标签: excel