Date Error 13 when Pulling from Blank Cell

Kirexin

New Member
Joined
Apr 27, 2015
Messages
6
When I run this code it works for each iteration of the loop until the duedate pulls from a blank cell. How do I fix this or make it so it skips blank cells? It gives "Runtime Error 13 Data Mismatch"

Code:
Sub CreateTask()
Dim olApp As New Outlook.Application
Dim olName As Outlook.Namespace
Dim olFolder As Outlook.Folder
Dim olTasks As Outlook.Items
Dim olNewTask As Outlook.TaskItem
Dim strSubject As String
Dim strDate As String
Dim DueDate As Date
Dim strBody As String
Dim reminderdate As String
Dim ws As Worksheet
Dim LR As Long
Dim i As Long
Dim DueCheck As Range

Set ws = Worksheets("KPI2") 'sheet where dates are
Set olName = olApp.GetNamespace("MAPI")
Set olFolder = olName.GetDefaultFolder(olFolderTasks)
Set olTasks = olFolder.Items

LR = ws.Range("E9").End(xlDown).Row

For i = 9 To LR
        strSubject = ws.Range("C" & i)  'takes subject from column c
        strDate = ws.Range("E" & i) 'takes date from column e
        strBody = ws.Range("C8") & Chr(10) & ws.Range("C" & i) & Chr(10) & Chr(10) & ws.Range("D8") & Chr(10) & ws.Range("D" & i) & Chr(10) & Chr(10) & ws.Range("F8") & Chr(10) & ws.Range("F" & i) & Chr(10) & Chr(10) & ws.Range("G8") & Chr(10) & ws.Range("G" & i) & Chr(10) & Chr(10) & ws.Range("K7") & Chr(10) & ws.Range("K" & i)
        Set olNewTask = olTasks.Add(olTaskItem) 'delete task if it exists 'an error is generated if task doesn't exist
        
        On Error Resume Next
        olTasks.Item (strSubject)
        If Err.Number = 0 Then
        olTasks.Item(strSubject).Delete
        End If
        On Error GoTo 0 'create new task
        
        With olNewTask
            .Subject = strSubject
            .Importance = olImportanceNormal
            .DueDate = DateValue(strDate)
            .Body = strBody
            .ReminderSet = True
            .Save
        End With
Next i
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi,
Try using the IsDate Function:

Rich (BB code):
For i = 9 To LR
        strSubject = ws.Range("C" & i)  'takes subject from column c
        strdate = ws.Range("E" & i) 'takes date from column e
        
    If IsDate(strdate) Then
    
    'rest of code
    
    
    
    End If
Next i

Dave
 
Upvote 0
Wow, thank you so much Dave, that fixed it! I'm going to go read up on that command so I can figure out why but thank you so much! It fixed my problem AND made my code faster.
 
Upvote 0
Wow, thank you so much Dave, that fixed it! I'm going to go read up on that command so I can figure out why but thank you so much! It fixed my problem AND made my code faster.

welcome thanks for feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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