英文:
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
' 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
答案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 ' 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
<sub>I do not advise this type of GUI tho, as it will confuse the user.</sub>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论