Here is the updated code which works for me in the actual file emailed to me...
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, strStock As String
Dim blnCreatedOL As Boolean, blnCY 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
MyWs.Range("A1").Value = Date + Time - tWait - 1
Else
If MyWs Is Nothing Then Set MyWs = ThisWorkbook.Sheets(sWsName)
If Len(MyWs.Range("A1").Value) = 0 Then MyWs.Range("A1").Value = Date + Time - tWait - 1
' 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, "O").End(xlUp).Row
For i = 2 To iLastRow
If IsError(Me.Cells(i, "O").Value) = False Then
If Me.Cells(i, "O").Value Like "Buy *" Then
strStock = strStock & Right$(Me.Cells(i, "O").Value, Len(Me.Cells(i, "O").Value) - 4) & sDelim
End If
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
If Dir("C:\Program Files\Express ClickYes\ClickYes.exe", vbNormal) = "" Then
blnCY = False
Else
Set oWShell = CreateObject("wscript.shell")
oWShell.Run ("""C:\Program Files\Express ClickYes\ClickYes.exe"" -activate")
blnCY = True
End If
'Create a new email message, add particulars
Set olMail = OL.CreateItem(0)
olMail.To = "8322983517@messaging.nextel.com"
olMail.Subject = "TIME TO BUY STOCK"
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")
If blnCY = True Then
olMail.Send
Else
olMail.display
End If
'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
If blnCY = True Then
oWShell.Run ("""C:\Program Files\Express ClickYes\ClickYes.exe"" -stop")
End If
ExitHere:
If blnCreatedOL = True Then OL.Quit
Call TOGGLEEVENTS(True)
End Sub The other two sub routines were put into a separate module named 'modFunctions'.