I have been working on an xlsm sheet that as part of its function produces a result of "No Data" in column J if it cannot find a match in its other data files.
What I need is to have Excel loop through Column J and automatically generate an email if the value in J = "No Data" and in the body of the email I need to include the cell offset value from Column F of the same Row.
I have used the Ron De Bruin code and modified it with Looping code from a similar function elsewhere in the project.
I cannot get this to function and could use some direction. Here is the code I have up to this point
Private Sub EmailIC()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'This macro adapted from: http://www.rondebruin.nl/win/s1/outlook/bmail4.htm
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim wbXLoc As String, wbX As Workbook, wsX As Worksheet, wsXName As String
Dim Xlr As Long
Dim rngX As Range, cel As Range, order As Range
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
wbXLoc = "C:\Users\Cole\Downloads\Dads Work\XDock\AutoXrpt.xlsm"
wsXName = "AutoX"
Set wsX = wbX.Sheets(wsXName)
'Loop through Column J to determine if = "No Data"
With wbX
Xlr = .Sheets("AutoX").Cells(Rows.Count, 1).End(xlUp).Row
Set rngX = wbX.Sheets("AutoX").Range("J2:J" & Xlr)
End With
'do the loop and find
For Each cel In rngX
If cel.Value = "No Data" Then
On Error Resume Next
With OutMail
.to = "robe******@msn.com"
.CC = ""
.BCC = ""
.Subject = "Need Pick Face please!"
.Body = rngX.cel.Offset(0, -4).Value
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End If
Next cel
End Sub
little confused to what you doing, but this should get you started-
What Om3r has looks good, they pointed out that you needed to set the wsX variable to an actual sheet before being able to set the range variable rngX. This might be why your loop might not have worked. Hard to say without knowing what error was thrown when you ran your code.
Also, be sure to have the object library for Outlook enabled. Check under the ribbon Tools>References and make sure your Outlook Library is listed.
you may want to try this (commented) code: