My VBA Code is not working properly

king0079z

New Member
Joined
Jan 1, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Dear all the VBA code is not working properly as it should send a reminder email if it checks there was "send Reminder" in the column to selected emails. The problem`s only working on row number one which contain " send "send Reminder" but another row no. Therefore I need your assistance in this issue

NO.Project DescriptionDue Date lRemarksEmail Address for the people who are responsible
1Project 11/1/2020Send Reminderemail1@outlook.com
2Project 21/4/2020email2@outlook.com
3Project 31/1/2020Send Reminderemail3@gmail.com
4Project 41/1/2020Send Reminderemail4@hotmail.com

The Code should send a reminder for all the email ist to the right if there is a send reminder in the remark column . Moreover, the project description should be included in the email subject and email body automatically
here is the code which I have written but it`s only working for Row 1 but I need it to work for all the rows in the worksheet.

VBA Code:
Sub SendReminderMail()
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim iCounter As Integer
Dim iCounter2 As Integer
Dim MailDest As String
Dim Subj As String

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)

With OutLookMailItem
MailDest = ""
Subj = ""
For iCounter = 1 To WorksheetFunction.CountA(Columns(4))
If MailDest = "" And Cells(iCounter, 4).Offset(0, -1) = "Send Reminder" Then
MailDest = Cells(iCounter, 4).Value
ElseIf MailDest <> "" And Cells(iCounter, 4).Offset(0, -1) = "Send Reminder" Then
MailDest = MailDest & ";" & Cells(iCounter, 4).Value
End If
Next iCounter
For iCounter2 = 1 To WorksheetFunction.CountA(Columns(4))
If Subj = "" And Cells(iCounter2, 4).Offset(0, -1) = "Send Reminder" Then
Subj = Cells(iCounter2, 1).Value
End If
Next iCounter2
.BCC = MailDest
.Subject = Subj
.Body = "Reminder: Your next credit card payment is due. Please ignore if already paid." & Subj
.Send
End With

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing
End Sub
 
Last edited by a moderator:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
To start with with you are only creating one outlookmailitem

You need to create a mail item for each email.

I am not sure of the rest of your logic it seems to duplication's of the same or similar tests
 
Upvote 0
Heres what i came up with
VBA Code:
Sub SendReminderMail()
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim iCounter As Integer
Dim iCounter2 As Integer
Dim MailDest As String
Dim Subj As String
Const mailcheck As String = "Send Reminder"
Dim mailchecktrue  As Boolean

Set OutLookApp = CreateObject("Outlook.application")


MailDest = ""
Subj = ""
For iCounter = 1 To WorksheetFunction.CountA(Columns(4))


    If Cells(iCounter, 4).Offset(0, -1) = mailcheck Then
        Set OutLookMailItem = OutLookApp.CreateItem(0)
        With OutLookMailItem
            MailDest = Cells(iCounter, 4).Value
            .To = MailDest
            .Subject = Cells(iCounter, 1).Value
            .Body = "Reminder: Your next credit card payment is due. Please ignore if already paid." & Cells(iCounter, 1).Value
            .display
        End With
    End If
Next iCounter

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing
End Sub
 
Last edited by a moderator:
Upvote 0
really thanks sir for your grate help


is it possible to end the email with a button without opening the outlook windows , so all the process can be from the excel
 
Upvote 0
great thanks sir for your assistance I ran the code but for every sends reminder is needed to allow the permission as per the attached photo, is there is a way I can only do it once and I will go through the whole cells
Capture33.PNG
 
Upvote 0
Try with simple:
with Application
.AskToUpdateLinks = False
.ScreenUpdating = False
.DisplayAlerts = False
end with
Just turn in on again after code run
 
Upvote 0
Here you go:
VBA Code:
Sub SendReminderMail()
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim iCounter As Integer
Dim iCounter2 As Integer
Dim MailDest As String
Dim Subj As String
Const mailcheck As String = "Send Reminder"
Dim mailchecktrue  As Boolean

with Application
.AskToUpdateLinks = False
.ScreenUpdating = False
.DisplayAlerts = False
end with 

Set OutLookApp = CreateObject("Outlook.application")


MailDest = ""
Subj = ""
For iCounter = 1 To WorksheetFunction.CountA(Columns(4))


    If Cells(iCounter, 4).Offset(0, -1) = mailcheck Then
        Set OutLookMailItem = OutLookApp.CreateItem(0)
        With OutLookMailItem
            MailDest = Cells(iCounter, 4).Value
            .To = MailDest
            .Subject = Cells(iCounter, 1).Value
            .Body = "Reminder: Your next credit card payment is due. Please ignore if already paid." & Cells(iCounter, 1).Value
            .display
        End With
    End If
Next iCounter

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing

with Application
.AskToUpdateLinks = True
.ScreenUpdating = True
.DisplayAlerts = True
end with 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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