Automation using VBA (Visual Basic for Applications) in Excel involves using VBA programming language to create macros, which are sets of instructions that automate repetitive tasks in Excel. Some common examples of tasks that can be automated using VBA in Excel include data entry, report generation, and formatting.
The benefits of automation using VBA in Excel include increased efficiency, improved accuracy, and the ability to complete complex tasks in a shorter amount of time. Additionally, by automating repetitive tasks, it frees up time for employees to focus on higher-level tasks that require critical thinking and decision-making skills.
In this data digest, I will walk you through the following highlighted below:
Note: The popular tableau superstore dataset will be leveraged for the purpose of this tutorial.
Let’s get started..
To activate the Developer tab in Excel, follow these steps:
Note: Once the Developer tab is activated, you can access Visual Basic Editor (VBE) by clicking on the "Developer" tab and then clicking on the "Visual Basic" button in the "Code" section.
This is a report that has been generated for different agent from the tableau dataset, I will be automating the mailing of this report to concerned stakeholders via mail in out look.
'This section converts the report into a picture
Sub send_email_with_table_as_pic()
'Outlook mail application
Dim OutApp As Object
Dim OutMail As Object
'Report table as a range
Dim table As Range
Dim pic As Picture
Dim pic1 As Picture
'The worksheet
Dim ws As Worksheet
'Other useful variables declaration
Dim wordDoc
Dim c As Range
Dim range1 As Range
Dim range2 As Range
Dim range3 As Range
Dim irow As Long
Dim Ldate As String
Dim sheet As Worksheet
Dim ccmail As String
Dim dCurrent_Monday As Date
Dim Tdate As Date
For Each c In ThisWorkbook.Worksheets("People").Range("c2:c4").Cells
If (c.Value) <> "" Then
Note: This means using a macro to automatically change the text (names in the dropdown of the report) in the report in alignment to respective sales manager
'Setting/opening outlook application
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Setting worksheet to automatically select each sales manager
Set ws = ThisWorkbook.Sheets("Report")
ws.Range("d2").Value = c.Offset(0, -2).Value
Application.Wait (Now + TimeValue("0:00:7"))
Set table = ws.Range("A1:i20")
table.Font.Size = 12
Ldate = Format(Now() - 1, "mmmm dd, yyyy")
'used in the body of the mail
'create email message
On Error Resume Next
With OutMail
.to = c.Value '& ";" & ccmail
.CC = ""
.BCC = ""
.Subject = "RM Report -" & " " & Format(Date, "DD-MM-yy")
Set wordDoc = OutMail.GetInspector.WordEditor
With wordDoc.Range
.PasteandFormat wdChartPicture
.insertParagraphAfter
table.Copy
wordDoc.Paragraphs(1).Range.PasteSpecial xlPasteFormats, , False, False
wordDoc.Content.insertParagraphAfter
.insertParagraphAfter
.insertParagraphAfter
End With
.display
.HTMLBody = "" & _
"Hi " & "" & c.Offset(0, -2).Value & "," & "<p>" & "Please see below your Sales performance as of " & Ldate & " and the recommended actions for your DSRs;" & "<p>" & " <p> " & .HTMLBody
'Adding time for buffer
Application.Wait (Now + TimeValue("0:00:3"))
.send
End With
On Error GoTo 0
End If
Application.CutCopyMode = False
Next c
MsgBox "Completed, please proceed"
Set OutApp = Nothing
Set OutMail = Nothing
End
Note: This kind of button is important in case the macro is to be used by someone who doesn’t understand how to run or manage the VBA. Basically for usability for non-technical users
Private Sub CommandButton1_Click()
Dim msgValue As VbMsggBoxResult
msgValue = MsgBox("Are you ready to send mail?" vbYesNo + vbQuestions, "Save")
if msgValue = vbYes Then
call send_email_with_table_as_pic()
VBA can be an effective tool for automating report sending in various contexts, particularly if you are working with Microsoft Excel or other Microsoft Office applications. With VBA, you can create macros that automate repetitive tasks, such as generating reports, formatting data, and sending emails.
However, using VBA for automation does require some technical knowledge and programming skills, so it may not be suitable for everyone. It's also important to be aware of potential security risks, such as malicious code or unauthorized access to sensitive data.
If you want to explore more on building your Microsoft Excel skill, visit our learning path HERE
Also, if you want to get started with data analytics and looking to improving your skills, you can check out our Learning Track
Empowering individuals and businesses with the tools to harness data, drive innovation, and achieve excellence in a digital world.
Copyright 2025Resagratia. All Rights Reserved.