VBA code to send an email if a certain IF Criteria is met that loops when the spreadsheet is open

joeyc123

New Member
Joined
Nov 5, 2014
Messages
11
Hi,
How can i create a vba code where it gives me an email alert if in my Sheet called "Summary" in column H2 to H18, contains the word "MAJOR". Range H2 to H18 contains an if statement that changes by the minute as It is a real-time formula so i would like this code to automatically run every minute if the sheet is opened and send me a email alert? is that possible? i have this code but it doesnt seem to automatically run every minute, what do you think is wrong?

Sub CheckCriteria()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range


Set ws = ThisWorkbook.Sheets("Summary")
Set rng = ws.Range("H2:H18")


For Each cell In rng
' Check if the cell contains the word "MAJOR"
If InStr(1, cell.Value, "MAJOR", vbTextCompare) > 0 Then
' If the criteria is met, send an email alert
SendEmailAlert
' Exit the loop since we found a match
Exit For
End If
Next cell

' Call the CheckCriteria subroutine again in one minute
Application.OnTime Now + TimeValue("00:01:00"), "CheckCriteria"
End Sub

Sub SendEmailAlert()
Dim OutlookApp As Object
Dim OutlookMail As Object

' Create a new Outlook application and email
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)

With OutlookMail
.To = "joey.cayago@gfigroup.com.ph"
.Subject = "Alert: MAJOR Detected in APAC MONITORING"
.Body = "The value 'MAJOR' has been detected in the APAC Spreadsheet Monitoring."
.Send
End With

Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
im getting this error message:
1707379049895.png
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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