Okay, this seems to work for me in testing...
Code:
Option Explicit
Dim MyWs As Worksheet
Private Sub Worksheet_Calculate()
Dim OL As Object, olMail As Object, oWShell As Object
Dim i As Long, iLastRow As Long
Dim strStock As String, blnCreatedOL As Boolean
Const sDelim As String = ";" 'stock [text] delimiter for multiple stocks
Const sWsName As String = "TimeTemp"
Const tWait As Long = 10 'length of time in minutes to wait before another email is dispatched (after calculate)
Call TOGGLEEVENTS(False)
'Create temporary worksheet if not already created
If SHEETEXISTS(sWsName, ThisWorkbook) = False Then
Set MyWs = ThisWorkbook.Sheets.Add(after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
MyWs.Name = sWsName
MyWs.Visible = xlSheetVeryHidden
Else
Debug.Print Format(Date + Time - TimeSerial(0, tWait, 0), "h:mm:ss AM/PM ddd, mmm d, yyyy") & " ~ " & MyWs.Range("A1").Value
If (Date + Time - TimeSerial(0, tWait, 0)) < MyWs.Range("A1").Value Then GoTo ExitHere
End If
'Find last row of data, loop through and grab those that say "Buy"
iLastRow = Me.Cells(Me.Rows.Count, "M").End(xlUp).Row
For i = 2 To iLastRow
If Me.Cells(i, "M").Value Like "Buy *" Then
strStock = strStock & Right$(Me.Cells(i, "M").Value, Len(Me.Cells(i, "M").Value) - 4) & sDelim
End If
Next i
If Right$(strStock, 1) = sDelim Then strStock = Left$(strStock, Len(strStock) - 1)
If Len(strStock) = 0 Then GoTo ExitHere
strStock = vbTab & Replace(strStock, sDelim, Chr(10) & vbTab)
'Create Outlook object
Set OL = GetObject(, "Outlook.Application")
blnCreatedOL = False
If OL Is Nothing Then
Set OL = CreateObject("Outlook.Application")
blnCreatedOL = True
End If
'Start ClickYes
Set oWShell = CreateObject("wscript.shell")
oWShell.Run ("""C:\Program Files\Express ClickYes\ClickYes.exe"" -activate")
'Create a new email message, add particulars
Set olMail = OL.CreateItem(0)
olMail.To = "your@addressHere.com"
olMail.Subject = "Add subject here"
olMail.Body = "These items were shown as 'Buy' items:" & vbNewLine & vbNewLine & _
strStock & vbNewLine & vbNewLine & _
"This email was created automatically on: " & Format(Date + Time, "ddd, mmm d, yyyy, h:mm AM/PM")
olMail.Send
'Set timer so we don't send an email after each cell calculates
MyWs.Range("A1").Value = Date + Time
MyWs.Range("A1").NumberFormat = "h:mm AM/PM ddd, mmm d, yyyy"
'Stop ClickYes if you want
oWShell.Run ("""C:\Program Files\Express ClickYes\ClickYes.exe"" -stop")
ExitHere:
If blnCreatedOL = True Then OL.Quit
Call TOGGLEEVENTS(True)
End Sub
Public Sub TOGGLEEVENTS(ByVal blnState As Boolean)
'Originally written by Zack Barresse
With Application
.Calculation = xlCalculationManual ' -4135 (manual)
.DisplayAlerts = blnState
.EnableEvents = blnState
.ScreenUpdating = blnState
If blnState Then .Calculation = xlCalculationAutomatic ' -4105 (auto)
If blnState Then .CutCopyMode = False
If blnState Then .StatusBar = False
End With
End Sub
Public Function SHEETEXISTS(wsName As String, Optional wkb As Workbook) As Boolean
If wkb Is Nothing Then
If ActiveWorkbook Is Nothing Then Exit Function
Set wkb = ActiveWorkbook
End If
On Error Resume Next
SHEETEXISTS = Len(wkb.Sheets(wsName).Name)
End Function This all goes into your worksheet module. So right click the sheet tab with your data, select View Code, past in there. Please make sure you look through the code because there are things you need to customize. I did put a list of the available stocks set to "Buy" in the body of the email in a delimited list. If this gets very long, it may make the email message lengthy.
There is one thing we didn't talk about although I did add for it. And that is on each cell calculation, if it meets the criteria, an email would be dispatched for each cell which meets your "Buy" criteria. This is not good (at least I'm assuming you don't want this). So in order to counteract that, I added a hidden sheet and set the last time an email was created in cell A1 of that sheet. Then when the routine is run it checks that cell for its value. If the current date/time is not greater than the value found plus the time to wait (set as a constant - which is the big reason you need to read the code!) then an email will not be created. In essence, you can run through one entire calculation and generate an email. Note that you may want to expand that time, which is a whole number and is expressed in minutes in the code. I have it defaulted to 10 minutes.
Set your email address and the subject. Note this does not have a lot of error handling or comments. I can adjust either one of those if you like, you just need to tell me. It assumes you have the ClickYes program installed, as well as Outlook. We can dummy-proof it as much as you want, again just let us know.
Let us know how this works for you.