Excel 中手动输入超时

huangapple go评论70阅读模式
英文:

Timeout on manual entry in Excel

问题

我想要用户在Excel单元格中输入数据,但如果在规定的时间内未完成输入,我想在超时时停止输入,并将截止到那时已经输入的内容输入到单元格中。

更新

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

Sub Button1_Click()

    Dim PauseTime, StartTime
    On Error Resume Next
    UserForm1.Show
    ' 设置持续时间(秒)
    PauseTime = 5
    ' 设置开始时间
    StartTime = Timer
    Do While Timer < StartTime + PauseTime
      DoEvents ' 让出给其他进程
    Loop
    ActiveCell.Value = UserForm1.TextBox1.Value
    Unload UserForm1 ' 关闭表单
    
End Sub
英文:

I want a user to enter data into an Excel cell, but if the entry but doesn't finish in a given time, I want to stop entry at that timeout, enter into the cell what ever has been typed up until that time.

Update

I tried the below code now but this does not work. It does not close the form or timeout.

<!-- language: vba -->

Sub Button1_Click()

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

答案1

得分: 0

Private Sub UserForm_Activate()
Dim PauseTime, StartTime

PauseTime = 5 ' 设置持续时间(秒)
StartTime = Timer ' 设置起始时间
Do While Timer < StartTime + PauseTime
DoEvents ' 让出CPU时间给其他进程
Loop
ActiveCell.Value = TextBox1.Value ' 将活动单元格的值设置为文本框中的内容
End ' 关闭窗体并结束程序

End Sub

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

英文:

If you want to use a userform and textbox for this, you can do it this way:

Private Sub UserForm_Activate()
  Dim PauseTime, StartTime
    
  PauseTime = 5                       &#39; Set duration in seconds
  StartTime = Timer                   &#39; Set start time
  Do While Timer &lt; StartTime + PauseTime
    DoEvents                          &#39; Yield to other processes
  Loop
  ActiveCell.Value = TextBox1.Value   &#39; set the active cell&#39;s value to what is in the textbox
  End                                 &#39; close the form and end the program

End Sub

<sub>I do not advise this type of GUI tho, as it will confuse the user.</sub>

huangapple
  • 本文由 发表于 2020年1月4日 12:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59588008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定