while loop to check value condition and send email

razhmo7al

New Member
Joined
Nov 17, 2018
Messages
11
Hi all,
i have been working on this project for week and couldn't find a way and i hope you can help me please.

i have excel sheet where has equipment with number of running days and if the cell reach certain value, it should send email to the responsible employee. i have about 25 equipments and the while loop has to check every row and check the running days if is equal or less than 60 days, it should send email automatically.

My problem is how to set the while loop to enter every cell and check the condition

i hope it's clear to understand and i have to submit this in 2 days.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,

I put equipments in columnA and amount of days left in Column B.

With this macro,

Code:
Sub Email()
[COLOR=#008000]'!!!In VBA go in Tools menu, references... and tick Microsoft Outlook (16.0) object library
' to be abble to send emails from excel
[/COLOR]
[COLOR=#008000]
'Declare Outlook[/COLOR]
Dim outlookApp As Outlook.Application
Dim myMail As Outlook.MailItem
Set outlookApp = New Outlook.Application
Set myMail = outlookApp.CreateItem(olMailItem)

Dim i As Long
[COLOR=#008000]'i is amount of columns to the left from the column of amounts of days to have the equipment name, in my example column A[/COLOR]
i = 1
[COLOR=#008000]
'Prepare the body of the mail[/COLOR]
Dim bdystr As String
[COLOR=#008000]'Begining of message[/COLOR]
bdystr = "Hi," & vbNewLine & vbNewLine & "Those are the equipments that need your attention:" & vbNewLine
[COLOR=#008000]'Loop through the cells
'Adapt B2:B16 to range with amounts of days[/COLOR]
For Each cell In Range("B2:B16")
    If cell.Value < 60 Then
    [COLOR=#008000]    ' Put a line with equiment A: x days left[/COLOR]
        bdystr = bdystr & vbNewLine & cell.Offset(0, -i).Value & " : " & cell.Value & " days left"
    End If
Next cell

[COLOR=#008000]'Add something at the end of the message[/COLOR]
bdystr = bdystr & vbNewLine & vbNewLine & "Thank you in advance!" & vbNewLine & "Best Regards,"

With myMail
    .To = "employee@mail.com"
    .Subject = "Check out those equipments!"
    .body = bdystr
    .display 'Replace .display by .send to send without seing it
End With
End Sub

I have an automatic mail looking like this

Capture.jpg
[/URL][/IMG]
 
Last edited:
Upvote 0
Thanks Kamolga for your Help.

The emails should be sent seperated for every equipment to different email address.
 
Upvote 0
Hi,

I put equipments in columnA and amount of days left in Column B.

With this macro,

Code:
Sub Email()
[COLOR=#008000]'!!!In VBA go in Tools menu, references... and tick Microsoft Outlook (16.0) object library
' to be abble to send emails from excel
[/COLOR]
[COLOR=#008000]
'Declare Outlook[/COLOR]
Dim outlookApp As Outlook.Application
Dim myMail As Outlook.MailItem
Set outlookApp = New Outlook.Application
Set myMail = outlookApp.CreateItem(olMailItem)

Dim i As Long
[COLOR=#008000]'i is amount of columns to the left from the column of amounts of days to have the equipment name, in my example column A[/COLOR]
i = 1
[COLOR=#008000]
'Prepare the body of the mail[/COLOR]
Dim bdystr As String
[COLOR=#008000]'Begining of message[/COLOR]
bdystr = "Hi," & vbNewLine & vbNewLine & "Those are the equipments that need your attention:" & vbNewLine
[COLOR=#008000]'Loop through the cells
'Adapt B2:B16 to range with amounts of days[/COLOR]
For Each cell In Range("B2:B16")
    If cell.Value < 60 Then
    [COLOR=#008000]    ' Put a line with equiment A: x days left[/COLOR]
        bdystr = bdystr & vbNewLine & cell.Offset(0, -i).Value & " : " & cell.Value & " days left"
    End If
Next cell

[COLOR=#008000]'Add something at the end of the message[/COLOR]
bdystr = bdystr & vbNewLine & vbNewLine & "Thank you in advance!" & vbNewLine & "Best Regards,"

With myMail
    .To = "employee@mail.com"
    .Subject = "Check out those equipments!"
    .body = bdystr
    .display 'Replace .display by .send to send without seing it
End With
End Sub

I have an automatic mail looking like this

Capture.jpg
[/URL][/IMG]



i have attached image of excel.

the loop should enter the cells and check if the running days equel or less than 60 should send email to responsible employee in the same row.

Orou7wd
 
Upvote 0
Code:
Sub Email()'!!!In VBA go in Tools menu, references... and tick Microsoft Outlook (16.0) object library
' to be abble to send emails from excel


'Declare Outlook
Dim outlookApp As Outlook.Application
Dim myMail As Outlook.MailItem
Set outlookApp = New Outlook.Application


'Prepare the body of the mail
Dim bdystr As String
'Loop through the cells
    For Each cell In Range("c2:C16")
        If (cell.Value < 60 And cell.Value <> "") Then
            Set myMail = outlookApp.CreateItem(olMailItem)
            'Begining of message
            bdystr = "Hi," & vbNewLine & vbNewLine & "Those are the equipments that need your attention:" & vbNewLine
            ' Put a line with equiment A: x days left
            bdystr = bdystr & vbNewLine & cell.Offset(0, -1).Value & " : " & cell.Value & " running days"
            'Add something at the end of the message
            bdystr = bdystr & vbNewLine & vbNewLine & "Thank you in advance!" & vbNewLine & "Best Regards,"
            With myMail
            .To = cell.Offset(0, 1).Value
            .Subject = "Check out those equipments!"
            .body = bdystr
            .display 'Replace .display by .send to send without seing it
            End With
        End If
    Next cell
End Sub
 
Upvote 0
Hi

Need your help again.
in the below code, how can i add between 2 values. Exampl: if the value <=60 and >57.

Sub Email()'!!!In VBA go in Tools menu, references... and tick Microsoft Outlook (16.0) object library
' to be abble to send emails from excel


'Declare Outlook
Dim outlookApp As Outlook.Application
Dim myMail As Outlook.MailItem
Set outlookApp = New Outlook.Application


'Prepare the body of the mail
Dim bdystr As String
'Loop through the cells
For Each cell In Range("c2:C16")
If (cell.Value < 60 And cell.Value <> "") Then
Set myMail = outlookApp.CreateItem(olMailItem)
'Begining of message
bdystr = "Hi," & vbNewLine & vbNewLine & "Those are the equipments that need your attention:" & vbNewLine
' Put a line with equiment A: x days left
bdystr = bdystr & vbNewLine & cell.Offset(0, -1).Value & " : " & cell.Value & " running days"
'Add something at the end of the message
bdystr = bdystr & vbNewLine & vbNewLine & "Thank you in advance!" & vbNewLine & "Best Regards,"
With myMail
.To = cell.Offset(0, 1).Value
.Subject = "Check out those equipments!"
.body = bdystr
.display 'Replace .display by .send to send without seing it
End With
End If
Next cell
End Sub
 
Upvote 0
Code:
[COLOR=#333333]If (cell.Value <= 60 And [/COLOR][COLOR=#333333]cell.Value>57[/COLOR][COLOR=#333333]) then [/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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