Send an e-mail based on the number of days until due

Even

Board Regular
Joined
Jan 1, 2013
Messages
81
Office Version
  1. 365
Platform
  1. Windows
Hi there, is it a way to generate an e-mail that includes the text I show beneath? When it is less den 90 days until renewal, I want to send out an e-mail to the person in charge of the customer about the renewal terms.

Send an automatic e-mail based on due date.xlsx
ABCDEFGHIJKLMNOPQR
15/17/2021
2General terms%Special termsCustomerDue dateTime until dueTransportD&OCyberCrimeArtName in chargeE-mail
3Transport4,50%5,00%Example 19/1/211074,50%3,50%5,00%
4D&O3,50%5,00%Example 211/30/211975,00%3,50%
5Cyber3,50%5,00%Example 37/1/21454,50%5,00%JohnJohn@test.com
6Crime3,50%5,00%Example 48/25/211005,00%5,00%
7Art3,50%5,00%Example 56/1/21154,50%3,50%5,00%JaneJane@test.com
8Example 67/30/21745,00%3,50%Jasonjason@test.com
9Example 77/1/21454,50%5,00%LydiaLydia@test.com
10Example 88/1/21765,00%5,00%EllenEllen@test.com
11Example 98/1/21763,50%
12Example 108/1/21763,50%
13
14
15Email SubjectRenewal
16Email BodyDear… it is soon time for renewal for X customer. The following standard terms of renewal is: Transport : 4,5% D&O: 3,5% Crime: 5% Please let me know if they will be renewing under these terms. Thank you! Sincerely X
17
All products
Cell Formulas
RangeFormula
A1A1=TODAY()
I3,I9,I7,I5I3=$B$3
L3,L10,L6:L7L3=$C$6
J3,J7J3=$B$4
J4,J10,J6,J8J4=$C$4
M4,M8M4=$B$7
K5,K9K5=$C$5
H3:H12H3=G3-$A$1
J11J11=B4
J12J12=B4
Cells with Data Validation
CellAllowCriteria
E3:E12List=$A$9:$A$11
 
That's correct. And together with when it's less than 90 days to due date (H).

It would be nice if the color changed as soon as I write Ok.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
That you can change with Conditional Formatting. For that matter, you could probably do all of the coloring with Conditional Formatting.
 
Upvote 0
I thouht about that. But sometimes when I do conditional formatting and use a macro to paste new data into the area, the conditional formatting is changed. Are you familiar with this? But I might do it wrong. I will try it again and see what happens.

Thanks.
 
Upvote 0
It looks like the conditional formatting is working so far. Thanks for the tips.
 
Upvote 0
I have been trying to search around for a code that only sends the visble rows, and I found that if the row height is 0 it means it is hidden. So I was thinking that if I use RowHeight > 0 Then before the code you made, it will send an e-mail that includes only the visible rows. But I don't how to do that. Could you help me with that?
 
Upvote 0
Replace this line in the code
VBA Code:
If rCell.Value < 91 And rCell.Height > 0 Then
 
Upvote 0
Thanks. So easy :)

I have tried to add one more range to code. And that is a column called "mail sendt". So the mail will be generated when 1. The number of days till due is less 90 days, 2. Ignore hidden rows and 3. When mail sendt column is blank. But why won't this one work? And is there an easy code to ignore cells that have an error? I found this one, but I am not sure if it works:
Code:
Application.ErrorCheckingOptions.BackgroundChecking = False

Code:
For Each rCell In Range(Cells(3, colTimeTillDue & colMailsendt), Cells(Rows.Count, colTimeTillDue & colMailsendt).End(xlUp))
If rCell.Value < 91 And rCell.Height > 0 & rCell.Value = """" Then
 
Upvote 0
What kind of error is showing (#N/A, #Value, etc.)? Also, in which cells are the errors? Why are the cells generating errors (for example, missing data in reference cell)?

I never knew this until now, but apparently, ampersanding two numbers concatenates them. So colTimeTillDue & colMailsendt will result in 1215 if they are 12 and 15, respectively. Weird. Anyway, that's not what you want to do: it will make the column 1215 (whatever letter combination that is) to be the one that row 3 is used from. Stick with just a single column to begin working on each row. Once you have the row via the rCell object, you can jump around the columns. For example, to go to the colMailsendt column, you can use Cells(rCell.row, colMailsendt).

It might be best to separate the tests simply so you can understand it a little better. We'll nest the If statements based on your ordering. If daysDue < 90, then if row not hidden, then if mailsendt is blank, then generate the email.

VBA Code:
    For Each rCell In Range(Cells(3, colTimeTillDue), Cells(Rows.Count, colTimeTillDue).End(xlUp))
        If rCell.Value < 91 Then
            If rCell.Height > 0 Then
                If Cells(rCell.Row, colMailsendt).Value = "" Then
                  
                    'put code here
                  
                End If
            End If
        End If
    Next rCell

Also, you tried to check for a blank value by using rCell.Value = """". This isn't actually a blank string. Instead, it is a string of a single double-quote: ". There are a couple different ways of having a string include the double-quote character. One is to use Chr(34), which is the ASCII code for that character. Often, I will put this character in a string like this simply to ensure I am aware of where the double-quotes are supposed to go:

VBA Code:
str = "Inserting the double-quote " & Chr(34) & " into a string."

This produces the string: Inserting the double-quote " into a string.

The other way is within a string of code (which requires the double-quotes for strings) by putting 2 back-to-back double-quotes. My same sentence above can then also be generated as:

VBA Code:
str = "Inserting the double-quote "" into a string."

The two double-quotes back to back do not end and start the string again right away. If you were to separate them by a space, then the compiler would be confused.

You might then see why you were comparing rCell.Value to a single double-quote. From the four you had next to each other, the first and last are required to designate a string; then it is left to the two inside the string, which produces a single double-quote in the string variable.
 
Upvote 0
Solution
You are great! VBA is not easy to me. How did you become so good?
 
Upvote 0
The file is starting to finally be finished thanks to you! Regarding the error, I just used the iferror formula and returned "".
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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