Trouble with If then statement for column information.

Realghostwheel

New Member
Joined
Apr 1, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Trying to set an auto email option for a large excel sheet, When I place a X in column 8 it generates an email and places a emailed note into column 11. I keep getting repeats for each X in column 8 though, Trying to set the script to also review that Column 11 has data, so that it only sends an email if Column 8 has an X and Column 11 is blank. Keep getting errors though.

For i = 1 To lRow
If (Cells(i, 8)) <> "" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

The above works to send the email but when trying to compare column 11 to ensure that it is blank is not working.

Option Explicit
Sub Email1()
Dim lRow As Integer
Dim i As Integer
Dim toDate As Date
Dim toList As String
Dim eSubject As String
Dim eBody As String
Dim Sheets As Worksheet
Dim OutApp, OutMail As Object


With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With


lRow = Cells(Rows.Count, 8).End(xlUp).Row


For i = 1 To lRow
If (Cells(i, 8)) <> "" Then '<--------------------------------------------- change this range
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


toList = Cells(i, 9) 'gets the recipient from col I
eSubject = "Contract Review"

eBody = "Hello" & "," & vbNewLine & vbNewLine & _
"A new PO has been entered and is ready for review PO " & Cells(i, 5) & vbNewLine & vbNewLine & _
"Sincerely, "


On Error Resume Next
With OutMail
.To = toList
.CC = ""
.BCC = ""
.Subject = eSubject
.Body = eBody

.Display ' ********* Creates draft emails. Comment this out when you are ready
'.Send '********** UN-comment this when you are ready to go live
End With

On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Cells(i, 11) = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column 11"
End If
Next i


ActiveWorkbook.Save


With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about:
VBA Code:
If (Cells(i, 8)) <> "" and Cells(i, 11) = "" Then '<--------------------------------------------- change this range
 
Upvote 0
JohnnyL,

Yes thank you see my problem now I had the two cell fields together with no And statement.

Eric
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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