Periodical check of cell value to email message

Quatron

New Member
Joined
Nov 8, 2017
Messages
3
Hello, I use the following code to send email when the cell value reaches my criteria:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub

If (Range("L10").Value > Range("M4").Value AND Range("M10").Value = "YES") OR (Range("L12").Value > Range("M4") AND Range("M12").Value = "YES") Then
Call Mail_Workbook
End If

End Sub



Sub Mail_Workbook()
Application.ScreenUpdating = False

Dim OutApp As Object
Dim OutMail As Object

'Create Initial variables
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

'Email
On Error Resume Next
With OutMail
.to = "mail@mail.com"
.CC = ""
.BCC = ""
.Subject = "Alert"
.Body = "Good value"
'.Attachments.Add (FilePath) 'Optional if you have an attachment
.Display
.Send 'Optional to automate sending of email.
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing

Application.ScreenUpdating = True
End Sub



However it needs to check the cell value every 5 minutes or so as the data in the cell is changing as it is pulled from the internet. How to change the code above to add the 5 minute check periodicity?

Any help is appreciated, thanks.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
This should do it. It will run the code in "check" every 10 minutes. Just run the "timer" sub when the workbook is opened.

Sub check()
'Your code here
Call timer
End Sub

Sub timer()
Application.OnTime Now + TimeValue("00:10:00"), "check"
End Sub
 
Upvote 0
Ended up using this found elsewhere on the internet:

In Workbook events:
<code>Private Sub Workbook_Open()
RunEveryTwoMinutes
End Sub</code> In a module:
<code>Sub RunEveryTwoMinutes()
//Add code here for whatever you want to happen
Application.OnTime Now + TimeValue("00:02:00"), "RunEveryTwoMinutes"
End Sub

The only issue now is that if the Outlook is not open the email is stuck in the Outbox. When Outlook is open it sends immediately. Any way to fix this?
</code>
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,938
Members
449,197
Latest member
k_bs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top