文字输入延迟

时间:2014-04-23 05:22:45

标签: excel vba excel-vba

我正在尝试在Excel中实现文本延迟输入。

用户需要从流式扫描仪获取一些数据 如果用户在3秒内再次扫描,则Excel需要清除当前单元格,并且不要将光标移动到下一个单元格上。

这是我的代码:

Public oldtime As Date
Public nowtime As Date

Private Sub Worksheet_Change(ByVal Target As Range)
nowtime = Now
    Application.EnableEvents = False
    If (nowtime - oldtime) > 0.0003 Then
        oldtime = Now
    Else
        ActiveCell.Value = ""
    End If
    Application.EnableEvents = True
End Sub

问题是它无法正常工作。有时它是清除细胞,但主要是清除细胞 - 不是 拜托,你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

无法使用扫描仪进行测试,但请尝试以下操作:

Option Explicit
Public oldtime As Variant
'Public nowtime As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
    If (Timer - oldtime) > 3 Then
        oldtime = Timer
    Else
        Target.Value = ""
        Target.Select
    End If
End Sub

这是你的尝试吗?