在下面的代码中,我在名为“chrono”的一系列单元格上捕获了双击事件。如果目标单元格包含一个值,我会将其与应用于右侧下一个单元格的公式中已包含的值连接起来。我想获得类似的东西=0,1+0,1
,但单元格仍然是空的。
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
On Error Resume Next
Dim rng As Range
Set rng = Range("chrono")
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, rng) Is Nothing Then
Application.EnableEvents = False
Cancel = True 'stop the edit mode
With Target
If .Value = "" Then
.Value = Time
Else:
Dim nextCell As Range
Set nextCell = ActiveCell.Offset(0, 1)
If nextCell.Formula = "" Then
nextCell.Formula = "=" & ((Time - .Value) * 24 * 60 * 60)
Else:
nextCell.Formula = nextCell.Formula & "+" & ((Time - .Value) * 24 * 60 * 60)
End If
.Value = ""
End If
End With
End If
Application.EnableEvents = True
End Sub
编辑
很抱歉不清楚,我的英语不太好。我想计算两次双击之间的经过时间(因此没有现有数据)。我能够通过这样做来完成这项任务:
nextCell.Value = Time - .Value
此外,我可以对多个输入求和:
If nextCell.Value = "" Then
nextCell.Value = Time - .Value
Else:
nextCell.Value = nextCell.Value + Time - .Value
问题是每个新输入都会覆盖nextCell.Value
,而我想跟踪每个输入。我尝试使用=t1+t2
第一个代码示例中公开的公式 (),但双击不会产生任何结果。
编辑
我正在尝试制作秒表。我的目标是计算在一项任务上花费的时间。为了让事情更清楚,这是我正在尝试做的事情,一步一步:
- 两个单元格:A1 和 B1
- 双击 A1
- A1值:当前时间
- 双击 A1
- B1公式:“=”&(当前时间-A1值)
- A1 值:空
- 重复 2、3、4
- B1公式:B1公式&“+”&(当前时间-A1值)
- 重复 2、3、4
- 等等...