Solved

Follow-up to "Generate Emails in Excel on Certain Days"

Posted on 2009-04-02
9
368 Views
Last Modified: 2012-06-27
I received help yesterday with a macro that generates emails fom an Excel sheet on a daily basis. The macro given to me worked fine, but when I modified my sheet and subsequently the macro I am getting and error. In particular I am getting a "type mismatch" error on this line:

If Now() - Cells(Cel.Row, "A") > 21 And LCase(Cells(Cel.Row, "E").Value) <> "yes" Then

[Now], of course, returns a Date type but [Cells(Cel.Row, "A")]  is returning a string with the value "Date". This because Line 1 is a header line. Can somebody please adjust the macro to start on Line 2 and terminate on the first blank line? Thank you.
Sub TranSEmail()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Cel As Range
    Dim i As Integer
    
 
    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    
    For Each Cel In Columns("D").Cells.SpecialCells(xlCellTypeConstants)
        If Now() - Cells(Cel.Row, "A") > 21 And LCase(Cells(Cel.Row, "E").Value) <> "yes" Then
            Set OutMail = OutApp.CreateItem(0)
            With OutMail
                .to = Cel.Value
                .Subject = "Order Number " & Cells(Cel.Row, "F").Value
                .Body = "Hello " & Cells(Cel.Row, "C").Value & "!" & vbNewLine & vbNewLine _
                    & "I am writing about your recent purchase of " & Cells(Cel.Row, "B").Value _
                    & ". I want to make sure all went well with your order. If you have any questions or concerns at all please let me know by email at bookitcorp@gmail.com. I appreciate this opportunity to serve you and sincerely hope you are having a nice day!" _
                    & vbNewLine & vbNewLine & "Very Sincerely Yours, " & vbNewLine & " Brian"
                .Display
            End With
            Set OutMail = Nothing
             Cells(Cel.Row, "E").Value = "Yes"
        End If
    Next Cel
    Application.ScreenUpdating = True
End Sub

Open in new window

0
Comment
Question by:bpfsr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 59

Expert Comment

by:Saurabh Singh Teotia
ID: 24049739
You can use the following code and it will do what you are looking for...
Saurabh...

Sub x()
 
 
 
 
End Sub
Sub TranSEmail()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Cel As Range
    Dim i As Integer
 
 
    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
 
    For Each Cel In Columns("D").Cells.SpecialCells(xlCellTypeConstants)
        If IsDate(cell.Value) = True Then
            If Now() - Cells(Cel.Row, "A") > 21 And LCase(Cells(Cel.Row, "E").Value) <> "yes" Then
                Set OutMail = OutApp.CreateItem(0)
                With OutMail
                    .to = Cel.Value
                    .Subject = "Order Number " & Cells(Cel.Row, "F").Value
                    .Body = "Hello " & Cells(Cel.Row, "C").Value & "!" & vbNewLine & vbNewLine _
                          & "I am writing about your recent purchase of " & Cells(Cel.Row, "B").Value _
                          & ". I want to make sure all went well with your order. If you have any questions or concerns at all please let me know by email at bookitcorp@gmail.com. I appreciate this opportunity to serve you and sincerely hope you are having a nice day!" _
                          & vbNewLine & vbNewLine & "Very Sincerely Yours, " & vbNewLine & " Brian"
                    .Display
                End With
                Set OutMail = Nothing
                Cells(Cel.Row, "E").Value = "Yes"
            End If
        End If
 
    Next Cel
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:bpfsr
ID: 24049832
Thank you, I am now getting Run-time error '424' - Object required on line:

If IsDate(cell.Value) = True Then

I'm thinking because the date column is actually A and the email column is D? Is the code above looking for dates in D or A?
0
 
LVL 59

Expert Comment

by:Saurabh Singh Teotia
ID: 24049891
Yes change this line to...
If IsDate(cell.Value) = True Then
to this...
If IsDate(cells(cell.row,"A").value) = True Then
Saurabh...
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:bpfsr
ID: 24050061
getting the same error on the same line - object required. Do I maybe need to set a range?
0
 
LVL 59

Expert Comment

by:Saurabh Singh Teotia
ID: 24050099
No you should not, can i see your workbook...
Saurabh...
0
 

Author Comment

by:bpfsr
ID: 24050515
Sure, thank you, here is a sample...
EE-Example.xls
0
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 500 total points
ID: 24050684
Ahh use this one and it will do what you are looking for...
Saurabh...

Sub TranSEmail()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Cel As Range
    Dim i As Integer
 
 
    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
 
    For Each Cel In Columns("D").Cells.SpecialCells(xlCellTypeConstants)
        If IsDate(Cells(Cel.Row, "A").Value) = True Then
            If Now() - Cells(Cel.Row, "A") > 21 And LCase(Cells(Cel.Row, "E").Value) <> "yes" Then
                Set OutMail = OutApp.CreateItem(0)
                With OutMail
                    .to = Cel.Value
                    .Subject = "Order Number " & Cells(Cel.Row, "F").Value
                    .Body = "Hello " & Cells(Cel.Row, "C").Value & "!" & vbNewLine & vbNewLine _
                          & "I am writing about your recent purchase of " & Cells(Cel.Row, "B").Value _
                          & ". I want to make sure all went well with your order. If you have any questions or concerns at all please let me know by email at bookitcorp@gmail.com. I appreciate this opportunity to serve you and sincerely hope you are having a nice day!" _
                          & vbNewLine & vbNewLine & "Very Sincerely Yours, " & vbNewLine & " Brian"
                    .Display
                End With
                Set OutMail = Nothing
                Cells(Cel.Row, "E").Value = "Yes"
            End If
        End If
 
    Next Cel
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:bpfsr
ID: 24055974
I'm sorry, I don't see where you made the change...
0
 

Author Comment

by:bpfsr
ID: 24056338
Okay I got it, thanks.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question