How to have msgbox appear when cell value hasn't changed in 30secs or 1min etc

萝らか妹 提交于 2020-07-23 06:35:41

问题


I am trying to make a msgbox appear when a cell value in range B4:B160 hasn't changed in 30secs or 1min etc. So what will appear is something like 'Have you scanned?' and will only appear if they haven't entered data in that cell in 30secs but will also close itself after 6mins or when they do enter data. It would be great if i can adjust these times in the code.

My spreadsheet is used for barcode scanning and is currently already running a userform when they enter the wrong data. Can I add another userform which is timed? or can I only have one going at a time?

Apologies a lot of questions but Ive been stuck on getting this to work for a week now and remembered this team at Stack Overflow is the best place to ask.

EDIT:

I have this code and is currently working but the timer is starting for each cell and pops up as many times as each of the changes. How do I get it to pop up only if one cell has changed? ALSO the duration is very inconsistent when I type in more than 10secs. I need this to be working for range B4-B168 not G Range as that is for another check that im running.

Private Sub Worksheet_change(ByVal Target As Range) Dim myCell As Range

For Each myCell In Range("G4:G168")

    If (Not IsEmpty(myCell)) And myCell.Value <> 17521 And myCell.Value <> "" Then

        DisplayUserForm1

        Exit Sub
    End If

Next myCell

startTimer

End Sub

Option Explicit
'Time before msgbox closes
Const PopupDurationSecs As Integer = 30

Sub startTimer()
'Time of msgbox before it opens
Application.OnTime Now + TimeValue("00:02:30"), "myShellMessageBox"
End Sub

Sub myShellMessageBox()

Dim Result As Integer
'.popup("strText",nSecondstowait,"strTitle",[nType])
Result = CreateObject("WScript.Shell").Popup("Have you scanned?", 
PopupDurationSecs, "Barcode Scan", 0 + 32)
'MsgBox Result
If Result = 1 Then
Exit Sub
End If
End Sub

回答1:


Duration = 30
TimeStart = Mid(15,2Now())
    

Do your thing here

TimeEnd = Mid(15,2Now())

If TimeEnd-TimeStart >= Durarion Then

Do your msgBox thing

VBA does run things from top to bottom, meaning that you’ll get a difference in time by getting Now() at different places in the code. This should to the trick.



来源:https://stackoverflow.com/questions/62683624/how-to-have-msgbox-appear-when-cell-value-hasnt-changed-in-30secs-or-1min-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!