首页 > 解决方案 > 在 Excel 中手动输入超时

问题描述

我希望用户将数据输入到 Excel 单元格中,但如果输入但未在给定时间内完成,我想在该超时时停止输入,将在此之前输入的内容输入到单元格中。

更新

我现在尝试了下面的代码,但这不起作用。它不会关闭表单或超时。

Sub Button1_Click()

    Dim PauseTime, StartTime
    On Error Resume Next
    UserForm1.Show
    ' Set duration in seconds
    PauseTime = 5  
    ' Set start time                     
    StartTime = Timer                   
    Do While Timer < StartTime + PauseTime
      DoEvents ' Yield to other processes
    Loop
    ActiveCell.Value = UserForm1.TextBox1.Value
    Unload UserForm1 ' close the form

End Sub

标签: excelvba

解决方案


如果你想为此使用用户表单和文本框,你可以这样做:

Private Sub UserForm_Activate()
  Dim PauseTime, StartTime

  PauseTime = 5                       ' Set duration in seconds
  StartTime = Timer                   ' Set start time
  Do While Timer < StartTime + PauseTime
    DoEvents                          ' Yield to other processes
  Loop
  ActiveCell.Value = TextBox1.Value   ' set the active cell's value to what is in the textbox
  End                                 ' close the form and end the program

End Sub

我不建议使用这种类型的 GUI,因为它会使用户感到困惑。


推荐阅读