Data Cleansing & Emailing: Invoice Schedule Report — Using Excel Macros
In this section, I’ve explained an Excel tool that can be utilised by the Accounts Receivable team to track and manage due invoices.
Often, the Accounts Receivable team has to track all invoices that are due, whether they, will be due in the future or are already due and accordingly, payment requests are sent to vendors or liable stakeholders.
Get Rishab Dang’s stories in your inbox
Join Medium for free to get updates from this writer.
In this Excel, we are doing the following steps using the 4 macro buttons:-
- First, it works by cleaning & massaging the data downloaded from SAP, in the format that is desired by the regional manager.
- Then, it segregates the data into three different sheets as per the due date of the invoice. “Currently Due”, Future Due”, “Past Due”
- Then, in the next click, it first, creates folders for each manager’s name.
- Then, it creates different Excel workbooks for different regions and pastes the new file into the newly created folder of each regional Manager. One manager can manage more than one region, therefore a folder can have multiple files.
- Draft and send an email to all those managers who currently have due or past due invoices with an attachment of complete details for their region.
Below is the list of VBA codes used to create the above macros :-
Data Cleansing
Sub part_2_preparing_files()
Application.ScreenUpdating = False
On Error GoTo errorhandlor
Dim lastrow As Long
Dim newlastrow As Long
Sheets("CC & FC").Select
Range("I1").Select
ActiveCell.FormulaR1C1 = "Given Company Code"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Finance Controller"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Email ID"
Range("N1").Value = "FC Email ID"
Range("I2").Select
ActiveCell.Formula2R1C1 = "=UNIQUE('Upcoming Invoices'!RC[-8]:R[99997]C[-8])"
Range("J2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],C[-9]:C[-4],5,0)"
Range("K2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],C[-10]:C[-5],6,0)"
lastrow = Sheets("CC & FC").Range("I" & Rows.Count).End(xlUp).Row - 1
Range("J2").Select
Selection.AutoFill Destination:=Range("J2:J" & lastrow)
Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K" & lastrow)
Range("M1").Select
ActiveCell.FormulaR1C1 = "Unique Controler"
Range("M2").Select
ActiveCell.Formula2R1C1 = "=UNIQUE(RC[-3]:R[3200]C[-3])"
newlastrow = Sheets("CC & FC").Range("M" & Rows.Count).End(xlUp).Row - 1
Range("N2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],C[-4]:C[-3],2,0)"
Range("N2").Select
Selection.AutoFill Destination:=Range("N2:N" & newlastrow)
Columns("I:N").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("I2").Select
Call creating_folders
Call creating_file
Application.ScreenUpdating = True
Exit Sub
errorhandlor:
MsgBox "An Unexcepted error has occured while creating folders, please check the user id and the name of the new column you have created on your desktop", vbCritical, "Productivity Team"
End SubCreating Folders on the Desktop
Sub creating_folders()
On Error Resume Next
Dim newlastrow As Long
Dim newfolder As String
Dim FCname As String
Dim xrow As Long
Dim xcol As Long
Dim i As Long
Dim myid As String
myid = Sheets("Home").Cells(2, 9).Value
xrow = 2
xcol = 13
newlastrow = Sheets("CC & FC").Range("M" & Rows.Count).End(xlUp).Row - 1
'------------------------------------------------------------------
For i = 1 To newlastrow - 1
FCname = Sheets("CC & FC").Cells(xrow, xcol).Value
newfolder = "C:\Users\" & myid & "\OneDrive - Kearney\Desktop\Invoice Schedule Files\" & FCname
MkDir newfolder
xrow = xrow + 1
Next i
End SubCreate excel files in newly created folders
Sub creating_file()
Dim newlastrow As Long
Dim newfolder As String
Dim CCname As String
Dim xrow As Long
Dim xcol As Long
Dim i As Long
Dim FCname As String
Dim currentwb As Workbook
Dim newwb As Workbook
Dim myid As String
myid = Sheets("Home").Cells(2, 9).Value
newlastrow = Sheets("CC & FC").Range("I" & Rows.Count).End(xlUp).Row - 1
xrow = 2
xcol = 9
Set currentwb = ThisWorkbook
For i = 1 To newlastrow - 1
CCname = Sheets("CC & FC").Cells(xrow, xcol).Value
On Error Resume Next
Sheets("Invoicing Due - Working").Select
Range("A1").Select
ActiveSheet.Range("$A$1:$T$99999").AutoFilter Field:=1, Criteria1:=CCname
Sheets("Past Due Invoices").Select
Range("A1").Select
ActiveSheet.Range("$A$1:$U$99999").AutoFilter Field:=1, Criteria1:=CCname
Sheets("Upcoming Invoices").Select
Range("A1").Select
ActiveSheet.Range("$A$1:$T$99999").AutoFilter Field:=1, Criteria1:=CCname
On Error GoTo 0
Set newwb = Workbooks.Add
newwb.Sheets.Add.Name = "Invoicing Due - Working"
newwb.Sheets.Add.Name = "Past Due Invoices"
newwb.Sheets.Add.Name = "Upcoming Invoices"
currentwb.Activate
Sheets("Invoicing Due - Working").Select
Range("A1:T99999").SpecialCells(xlCellTypeVisible).Copy
newwb.Activate
Sheets("Invoicing Due - Working").Select
Range("A1").Select
ActiveSheet.Paste
currentwb.Activate
Sheets("Past Due Invoices").Select
Range("A1:U99999").SpecialCells(xlCellTypeVisible).Copy
newwb.Activate
Sheets("Past Due Invoices").Select
Range("A1").Select
ActiveSheet.Paste
currentwb.Activate
Sheets("Invoicing Due - Working").Select
Range("A1:T99999").SpecialCells(xlCellTypeVisible).Copy
newwb.Activate
Sheets("Upcoming Invoices").Select
Range("A1").Select
ActiveSheet.Paste
currentwb.Activate
FCname = Sheets("CC & FC").Cells(xrow, 10).Value
newwb.Activate
newwb.SaveAs "C:\Users\" & myid & "\OneDrive - Kearney\Desktop\Invoice Schedule Files\" & FCname & "\" & CCname & ".xlsx"
newwb.Close
xrow = xrow + 1
Next i
MsgBox "Files & Folders have been created on your desktop", vbInformation, "Productivity Team"
End SubDraft & send emails with attachments
Sub AttachFilesToEmail()
Application.ScreenUpdating = False
'On Error GoTo error_handlor
Dim objOutlook As Object
Dim objMail As Object
Dim objFileSystem As Object
Dim objFolder As Object
Dim objFile As Object
Dim strFolderPath As String
Dim strFileName As String
Dim FCname As String
Dim i As Long
Dim xrow As Long
Dim xcol As Long
Dim newlastrow As Long
Dim FCEmail As String
Dim emailbody As String
emailbody = " Please find attached Excel files." & vbNewLine & "Regards"
xrow = 2
xcol = 13
newlastrow = Sheets("CC & FC").Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To newlastrow - 2
FCname = Sheets("CC & FC").Cells(xrow, xcol).Value
FCEmail = Sheets("CC & FC").Cells(xrow, 14).Value
strFolderPath = "C:\Users\rdang01\OneDrive - Kearney\Desktop\Invoice Schedule Files\" & FCname & "\" 'check if \ is required in the end
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With objMail
.Subject = "Invoice Schedule Report "
.Body = "Hi " & FCname & emailbody
.To = FCEmail
End With
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFileSystem.GetFolder(strFolderPath)
' Loop through each file in the folder
For Each objFile In objFolder.Files
' Check if the file is an Excel file
If LCase(Right(objFile.Name, 5)) = ".xlsx" Or LCase(Right(objFile.Name, 4)) = ".xls" Then
' Attach the file to the email
objMail.Attachments.Add objFile.Path
End If
Next objFile
objMail.Display
' Clean up objects
Set objMail = Nothing
Set objOutlook = Nothing
Set objFileSystem = Nothing
Set objFolder = Nothing
Set objFile = Nothing
xrow = xrow + 1
Next i
Application.ScreenUpdating = True
MsgBox "Emails have been sucessfully created", vbInformation, "Productivity Team"
Exit Sub
error_handlor:
MsgBox "An Unexpected error has occurred, Kindly check the names in the folders and the names in CC&FC sheet column M& N are correct", vbCritical, "Productivity Team"
End SubRemoving old data
Sub remove_old_data()
Application.ScreenUpdating = False
Sheets("Invoicing Due - Working").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A2").Select
Sheets("Past Due Invoices").Select
Application.Goto Reference:="R2C1"
ActiveWindow.SmallScroll Down:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A2").Select
Sheets("Upcoming Invoices").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A2").Select
Sheets("Raw - Invoice Schedule").Select
Cells.Select
Range("I1").Activate
Selection.Delete Shift:=xlUp
Range("A1").Select
Sheets("CC & FC").Select
Range("I:N").Select
Selection.ClearContents
Range("A1").Select
Application.ScreenUpdating = True
Sheets("Home").Select
MsgBox "Tool has been cleaned. Please input new data to run the report", vbInformation, "Productivity Initiative Team"
End Sub







