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