VBA generates word docs, request to amend code to include description of deadlines/tasks due

ttp921

New Member
Joined
Sep 8, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
My VBA macro runs reports from Excel to generate a Word doc with upcoming deadlines/due dates. However, I can't figure out how to include a description of the upcoming deadlines/due dates when I run macros. Here are the columns that I need help with:

COLUMN A = MATTER NAME
COLUMN K = TASK DATE
COLUMN L = +/- DAYS
COLUMN M = TASK DUE
COLUMN N = TASK DUE DESCRIPTION

Currently, the code returns a result like this: SMITH, JOHN 4-16-2021 - TASK DUE - 9/23/2023. Is it possible to amend the code to include COLUMN N = TASK DUE DESCRIPTION so that the report looks like this instead: SMITH, JOHN 4-16-2021 - TASK DUE - 9/23/2023 - TASK DUE DESCRIPTION - CALL CLIENT?

I've uploaded a copy of the Excel sheet along with an example of the generated Woc doc report. Here's the code that I am currently using:

Sub Due_Date()
Dim DueDate_Col As Range
Dim Due As Range
Dim PopUp_Notification As String
Set DueDate_Col = Range("J2:J500,D2:D500,D2:D500,M2:M500")
For Each Due In DueDate_Col
If Due <> "" And Date >= Due - Range("S2") Then
PopUp_Notification = PopUp_Notification & vbCrLf & Cells(Due.Row, "A") & " - " & Cells(1, Due.Column) & " - " & Due.Value
End If

Next Due
If PopUp_Notification = "" Then
MsgBox "No outstanding tasks/SOLs due."
Else
Dim WordApp As Object
Dim WDoc As Object
'
'Create Word.Application
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
Err.Clear
End If
On Error GoTo 0
WordApp.Visible = True
Set WDoc = WordApp.Documents.Add
WDoc.Range(0, 0).Text = PopUp_Notification
WordApp.Activate
End If
End Sub

CASE DEADLINES EXCEL.xlsm
ABCDEFGHIJKLMN
1MATTER NAMEINCIDENT DATE+/- YRSSOL EXP. DATEDISPOSED DATENEW SOL EXP. DATESUB-STATUSLAST CONTACT DATE+/- DAYSCLT CONTACT DUETASK DATE+/- DAYSTASK DUETASK DUE DESCRIPTION
2SMITH, JOHN 4-16-20214/16/202124/16/202310/27/202110/27/2023INTAKE9/28/20233010/28/20238/24/2023309/23/2023CALL CLIENT
3SMITH, JANE 05-15-202105/15/202125/15/20233/30/20223/30/2024INTAKE9/27/202329/29/20239/27/202329/29/2023CALL CLIENT AND GET ADDITIONAL INFORMATION
4SMITH, CHRIS 05-15-202105/15/202125/15/20233/30/20223/30/2024CLOSE FILE 9/27/20233010/27/20238/24/2023309/23/2023CALL CLIENT TO CLOSE FILE
Sheet 1
Cell Formulas
RangeFormula
D2:D4D2=DATE(YEAR(B2)+C2,MONTH(B2),DAY(B2))
F2:F4F2=DATE(YEAR(E2)+C2,MONTH(E2),DAY(E2))
J2:J4,M2:M4J2=H2+I2
Cells with Conditional Formatting
CellConditionCell FormatStop If True
M:MCell Value<TODAY()textNO
M:MCell Value=TODAY()textNO
M:MCell Valuebetween TODAY() and TODAY()+5textNO
M:MCell Value>TODAY()textNO
D:DCell Value<TODAY()textNO
J:JCell Value<TODAY()textNO
J:JCell Value=TODAY()textNO
D:DCell Value=TODAY()textNO
D:DCell Valuebetween TODAY() and TODAY()+30textNO
J:JCell Valuebetween TODAY() and TODAY()+30textNO
D:DCell Valuebetween TODAY() and TODAY()+90textNO
D:DCell Valuebetween TODAY() and TODAY()+180textNO
D:DCell Value>TODAY()textNO
J:JCell Valuebetween TODAY() and TODAY()+90textNO
J:JCell Valuebetween TODAY() and TODAY()+180textNO
J:JCell Value>=TODAY()+1textNO
 

Attachments

  • Excel Screenshot.png
    Excel Screenshot.png
    30.7 KB · Views: 4

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
A couple of things I noticed:
- It loops through 500 rows. This could be improved to only run through the number of rows that have data. Are there ever blank cells in column A followed by valid data in lower rows? (i.e. row 10 would have nothing in A10 and then row 11 and below might have more data)
- When the code generates the PopUp_Notification, it uses Due.Value. When it does this, it gives you the date from Column J (CLT Contact Due) and not Column M (Task Due). Is this the correct date? Your example above suggested otherwise.

Assuming you don't want to change any of the above issues, you would change 1 line:
VBA Code:
PopUp_Notification = PopUp_Notification & vbCrLf & Cells(Due.Row, "A") & " - " & Cells(1, Due.Column) & " - " & Due.Value & " - " & Cells(1, "N") & " - " & Cells(Due.Row, "N")

If you want it to actually reference the TASK DUE (Column M) you would change it to:
VBA Code:
PopUp_Notification = PopUp_Notification & vbCrLf & Cells(Due.Row, "A") & " - " & Cells(1, "M") & " - " & Cells(Due.Row, "M") & " - " & Cells(1, "N") & " - " & Cells(Due.Row, "N")

There are some other streamlining things that could be done if it runs slow, but if speed is not an issue it might not be worth messing with.
 
Upvote 0
A couple of things I noticed:
- It loops through 500 rows. This could be improved to only run through the number of rows that have data. Are there ever blank cells in column A followed by valid data in lower rows? (i.e. row 10 would have nothing in A10 and then row 11 and below might have more data)
- When the code generates the PopUp_Notification, it uses Due.Value. When it does this, it gives you the date from Column J (CLT Contact Due) and not Column M (Task Due). Is this the correct date? Your example above suggested otherwise.

Assuming you don't want to change any of the above issues, you would change 1 line:
VBA Code:
PopUp_Notification = PopUp_Notification & vbCrLf & Cells(Due.Row, "A") & " - " & Cells(1, Due.Column) & " - " & Due.Value & " - " & Cells(1, "N") & " - " & Cells(Due.Row, "N")

If you want it to actually reference the TASK DUE (Column M) you would change it to:
VBA Code:
PopUp_Notification = PopUp_Notification & vbCrLf & Cells(Due.Row, "A") & " - " & Cells(1, "M") & " - " & Cells(Due.Row, "M") & " - " & Cells(1, "N") & " - " & Cells(Due.Row, "N")

There are some other streamlining things that could be done if it runs slow, but if speed is not an issue it might not be worth messing with.

Thank you for your response! In Column A, there are not usually any blank cells. I believe it's coded for 500 rows for any new clients that are added to my sheet. Regarding your second question, I wanted the code to generate 3 dates and descriptions, using these columns:

COLUMN D = SOL EXPIRATION DATE
COLUMN J = CLT CONTACT DUE
COLUMN M = TASK DUE
COLUMN N = TASK DUE DESCRIPTION

As such, I used your code to replace line 1 and it works (huzzah!)! The word doc report now shows the task due description, however, it creates two different entries by splitting the contacts and the tasks due into two separate sections on the report instead of showing both dates (COLUMN J and COLUMN N) on the same line. Here's what it looks like:

SMITH, CHRIS 05-15-2021 - CLT CONTACT DUE - 10/27/2023 - TASK DUE DESCRIPTION - CALL CLIENT TO CLOSE FILE
SMITH, CHRIS 05-15-2021 - TASK DUE - 9/23/2023 - TASK DUE DESCRIPTION - CALL CLIENT TO CLOSE FILE

Would it be possible to either include both due dates in a single line like this: SMITH, CHRIS 05-15-2021 - CLT CONTACT DUE - 10/27/2023 - TASK DUE - 9/23/2023 - TASK DUE DESCRIPTION - CALL CLIENT TO CLOSE FILE?

Note, the report currently separates out COLUMN D into its own section and that is okay with me. I was more concerned about getting the description to generate in the report.

Thank you very much for your help!
 
Upvote 0
Make sure you only have 1 PopUp_Notification statement. To do what you were asking for, you should be able to replace the PopUp_Notification with:
VBA Code:
            PopUp_Notification = PopUp_Notification & vbCrLf & Cells(Due.Row, "A") & " - " & Cells(1, "J") & " - " & Cells(Due.Row, "J") _
                & " - " & Cells(1, "M") & " - " & Cells(Due.Row, "M") & " - " & Cells(1, "N") & " - " & Cells(Due.Row, "N")

The whole sub would be:
VBA Code:
Sub Due_Date()
Dim DueDate_Col As Range
Dim Due As Range
Dim PopUp_Notification As String
    
    Set DueDate_Col = Range("J2:J500,D2:D500,D2:D500,M2:M500")
    For Each Due In DueDate_Col
        If Due <> "" And Date >= Due - Range("S2") Then
            PopUp_Notification = PopUp_Notification & vbCrLf & Cells(Due.Row, "A") & " - " & Cells(1, "J") & " - " & Cells(Due.Row, "J") _
                & " - " & Cells(1, "M") & " - " & Cells(Due.Row, "M") & " - " & Cells(1, "N") & " - " & Cells(Due.Row, "N")
        End If
    Next Due
    If PopUp_Notification = "" Then
        MsgBox "No outstanding tasks/SOLs due."
    Else
    Dim WordApp As Object
    Dim WDoc As Object
    '
    'Create Word.Application
    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set WordApp = CreateObject("Word.Application")
        Err.Clear
    End If
    On Error GoTo 0
    WordApp.Visible = True
    Set WDoc = WordApp.Documents.Add
    WDoc.Range(0, 0).Text = PopUp_Notification
    WordApp.Activate
    End If
End Sub

If you need to add Column D, that would be this sub:

VBA Code:
Sub Due_Date()
Dim DueDate_Col As Range
Dim Due As Range
Dim PopUp_Notification As String
    
    Set DueDate_Col = Range("J2:J500,D2:D500,D2:D500,M2:M500")
    For Each Due In DueDate_Col
        If Due <> "" And Date >= Due - Range("S2") Then
            PopUp_Notification = PopUp_Notification & vbCrLf & Cells(Due.Row, "A") & " - " & Cells(1, "D") & " - " & Cells(Due.Row, "D") _
                & " - " & Cells(1, "J") & " - " & Cells(Due.Row, "J") & " - " & Cells(1, "M") & " - " & Cells(Due.Row, "M") & " - " & Cells(1, "N") & " - " & Cells(Due.Row, "N")
        End If
    Next Due
    If PopUp_Notification = "" Then
        MsgBox "No outstanding tasks/SOLs due."
    Else
    Dim WordApp As Object
    Dim WDoc As Object
    '
    'Create Word.Application
    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set WordApp = CreateObject("Word.Application")
        Err.Clear
    End If
    On Error GoTo 0
    WordApp.Visible = True
    Set WDoc = WordApp.Documents.Add
    WDoc.Range(0, 0).Text = PopUp_Notification
    WordApp.Activate
    End If
End Sub
 
Upvote 1
Solution
Thanks so much for all of your help! Is there anyway to have the report generate the deadlines in COLUMN N and COLUMN J in chronological order? It is difficult sort and prioritize the tasks due in the generated report.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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