VBA - DueDate and Next Without For errors

Kirexin

New Member
Joined
Apr 27, 2015
Messages
6
Hey guys, great forum, I've been using it a lot while doing this work in Excel.

I am writing code to have Tasks in Outlook created by a huge list of tasks in an Excel document. I want to skip all the blank rows and rows of assignments that are already completed. So I am trying to make it so if the Due Date column (Column E) is NOT blank and the Actual Completion Date column (Column E) IS blank then it should create an outlook task. I have it so it is able to create outlook tasks but I am running into two problems.

1) In the section right under "For i = 9 to 25000" where it is supposed to ignore the row based on the above mentioned criteria it gives me the error "Compile error: Next without For" on the line that says Next i

2) In the section where I am trying to assign a due date near the bottom it gives me an Error 13 for the line .DueDate = DateValue(strDate)

Here is the code
Code:
Sub CheckBinding()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
MsgBox olApp.Name
End Sub

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 strBody As String
Dim reminderdate As String
Dim ws As Worksheet
Dim i As Long
Dim DueCheck As Range


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


LR = ws.Range("C9").End(xlDown).Row 'get row for last cell in column D with value


For i = 9 To 25000 'assuming the rows have headers, so loop starts on row 2
   If Not IsEmpty(ws.Range("E", i)) And ws.Range("J", i) = "" Then
        Next i
    Else
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 olNewTaskthat
Subject = strSubject
.Importance = olImportanceNormal
.DueDate = DateValue(strDate)
.Body = strBody
.ReminderSet = True
.Save
End With
    End If
Next i
End Sub

Sub CreateOutlookTask()
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Your description was a bit vague (and contradictory), but I believe this may be what you are looking for.

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 strBody As String
Dim reminderdate As String
Dim ws As Worksheet
Dim i As Long
Dim DueCheck As Range

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

LR = ws.Range("C9").End(xlDown).Row 'get row for last cell in column D with value

For i = 9 To 25000 'assuming the rows have headers, so loop starts on row 2
   If Not IsEmpty(ws.Range("E" & i)) And ws.Range("J" & i) = "" Then
        
        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
    End If
Next i
End Sub
 
Upvote 0
Thanks BiocideJ. It is picking up the correct days for the due date but it is still saying error 13. I think that when the loop gets to a blank cell it is trying to read it as a date and it isn't work. What do you think?
 
Upvote 0
I've tried a number of ways for it to skip the ranges where the Due Date (column E) are blank but I can't get it to work. I can remove the duedate part and it runs fine except it obviously doesn't have a duedate in outlook or I can leave it in and it works fine until it encounters the first blank cell in the due date column.
 
Upvote 0
You really don't say what you expect to happen with a blank due date, so I am going to assume you will add the task without a Due Date.

In that case change this:
.DueDate = DateValue(strDate)

to this:
If strDate <> "" Then .DueDate = DateValue(strDate)
 
Upvote 0
OK, perhaps this amendment to the IF statement will cause the test to be more reliable.
If this still doesn't work, perhaps you can better define what is in column E. (i.e. typed values, formulas, etc.)

NOTE: This code will still error out if there is a non-blank, non-date value in column E. If you expect that that will happen, let me know and I will try to make a work-around.

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 strBody As String
Dim reminderdate As String
Dim ws As Worksheet
Dim i As Long
Dim DueCheck As Range
Set ws = Worksheets("KPI") 'sheet where dates are
Set olName = olApp.GetNamespace("MAPI")
Set olFolder = olName.GetDefaultFolder(olFolderTasks)
Set olTasks = olFolder.Items
LR = ws.Range("C9").End(xlDown).Row 'get row for last cell in column D with value
For i = 9 To 25000 'assuming the rows have headers, so loop starts on row 2
    strDate = ws.Range("E" & i) 'takes date from column e
    If strDate <> "" And ws.Range("J" & i) = "" Then
        
        strSubject = ws.Range("C" & i)  'takes subject from column c
        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
    End If
Next i
End Sub
 
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