Run Time Error 13 - Type Mismatch - How to ignore Blank Dates??

StephenMatthews

New Member
Joined
Sep 26, 2013
Messages
28
Hi,

I keep getting a Run Time Error 13 – Type Mismatch on the below Highlighted Code below when a date field is blank, how can I add some additional code to ignore blank dates? The Macro is used so that when a Certificate date expires that 7 days prior to this a reminder e-mail is sent, some lines of data don’t have expiry dates hence they are blank and causing this error.

Thanks

Stephen.
Code:
Sub eMail()
Dim lRow As Integer
Dim i As Integer
Dim toDate As Date
Dim toList As String
Dim eSubject As String
Dim eBody As String
 
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
End With
 
Sheets(1).Select
lRow = Cells(Rows.Count, 5).End(xlUp).Row
 
For i = 2 To lRow
toDate = Replace(Cells(i, 16), ".", "/")
  If Left(Cells(i, 18), 5) <> "Mail" And toDate - Date <= 7 Then
     Set OutApp = CreateObject("Outlook.Application")
     Set OutMail = OutApp.CreateItem(0)
 
        toList = Cells(i, 5)
        eSubject = "Certificate Out of Date - " & Cells(i, 1) & " is due on " & Cells(i, 16)
        eBody = "Hi Maz" & vbCrLf & vbCrLf & "Certificate is due for renewal."
        
        On Error Resume Next
        With OutMail
        .To = toList
        .CC = ""
        .BCC = ""
        .Subject = eSubject
        .Body = eBody
        .bodyformat = 1
        '.Display
        .Send
        End With
 
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
 Cells(i, 17) = "Mail Sent " & Date + Time
End If
Next i
 
ActiveWorkbook.Save
 
With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True
End With
End Sub
 
Last edited by a moderator:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The problem line doesn't appear to be highlighted, which line is it?
 
Upvote 0
Stephen

If you want to skip creating/sending the email if the date cell is blank try this.
Code:
Sub eMail()
Dim lRow As Integer
Dim i As Integer
Dim toDate As Date
Dim toList As String
Dim eSubject As String
Dim eBody As String

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

    Sheets(1).Select
    lRow = Cells(Rows.Count, 5).End(xlUp).Row

    For i = 2 To lRow
    
        If Cells(i, 16) <> "" Then
        
            toDate = Replace(Cells(i, 16), ".", "/")
            
            If Left(Cells(i, 18), 5) <> "Mail" And toDate - Date <= 7 Then
                Set OutApp = CreateObject("Outlook.Application")
                Set OutMail = OutApp.CreateItem(0)

                toList = Cells(i, 5)
                eSubject = "Certificate Out of Date - " & Cells(i, 1) & " is due on " & Cells(i, 16)
                eBody = "Hi Maz" & vbCrLf & vbCrLf & "Certificate is due for renewal."

                On Error Resume Next
                With OutMail
                    .To = toList
                    .CC = ""
                    .BCC = ""
                    .Subject = eSubject
                    .Body = eBody
                    .bodyformat = 1
                    '.Display
                    .Send
                End With

                On Error GoTo 0
                Set OutMail = Nothing
                Set OutApp = Nothing
                Cells(i, 17) = "Mail Sent " & Date + Time
            End If
            
        End If
        
    Next i

    ActiveWorkbook.Save

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

PS Are you sure you need to create a new instance of Outlook for every email?

Try moving this outside the loop.
Code:
Set OutApp = CreateObject("Outlook.Application")
 
Upvote 0
This worked, great thanks and in answer to your last point, I require separate e-mils for each event, thanks for the Super quick reply.

Regards

Stephen.
 
Last edited by a moderator:
Upvote 0
A separate email doesn't require trying to start Outlook inside the loop. I suggest you follow Norie's advice and move that line before the loop.
 
Upvote 0
A separate email doesn't require trying to start Outlook inside the loop. I suggest you follow Norie's advice and move that line before the loop.
 
Upvote 0
Hi Rory,

Before the Loop?? Can you advise where that is, Stupid question but only have basic knowledge of Macros etc.

Thanks and appreciate All your help.

Regards

Stephen.
 
Upvote 0
Sure. Move this line:

Code:
Set OutApp = CreateObject("Outlook.Application")

so that it is before this line:

Code:
For i = 2 To lRow
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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