In this article, we are going to learn how to automate the mail merge by using the VBA in Microsoft Excel.
Mail Merge: - This is the source to merge the data’s information into text and then print the document. To perform such operation, we use Microsoft Word.
Let’s understand with a simple exercise:-
We have a letter format in Report sheet, and we want to apply mail merge through VBA in which we want to change the letter details as well.
We have 2 sheets. 1 sheet contains data with details to whom we want to give letters. In first data, column A contains Name, column B contains street address, column C contains city, column D region, and column E and column F contain postal zip. There is one command button to move in the report sheet.
Did you know you can create an Outlook email from Excel? You can even create it with an Excel formula using cell references for mailto, subject and cc? By default, Microsoft Excel 2019, Word 2019, and Office 365 doesn’t have the useful “ Send to Mail Recipient ” option available on the toolbar. The option enables users to quickly access their email client to send documents. Here’s how to add it. Excel-VBA: Send Unique Images Embedded to Mail Body, With Every Mail From MS Outlook using Excel. Excel-VBA: Send Mail with Embedded Image in message body From MS Outlook using Excel. Excel-VBA: Send a Excel Workbook as Attachment in Mail From MS Outlook Using Excel; Excel-VBA: Open a Excel File using Another Excel File using Browse Option.
2nd sheet is having the letter format with 2 command buttons; one button to move on the data sheet and second command button is to work for mail merge
Firstly, we will write the VBA code for command button of Main Data. We need to follow below given steps:-
- First we will insert command button in the worksheet.
- Go to Developer tab and then insert Command button from Activexcontrol.
- Rename the Command button with the name “Letter” , and now assign below mentioned macro:-
Private Sub Main_data_Click()
Worksheets('Report').Activate
Range('A19').Show
End Sub
Now, we will insert the second command button in the Report sheet and assign the macro to move on the first sheet. We need to follow below given steps:-
- Rename the Command button with the name “Data” , and assign below mentioned macro:-
Private Sub CommandButton2_Click()
Worksheets('Main_Data').Activate
Range('A1').Show
End Sub
Excel Mailing List Template
Now we will write the main code for mail merge by following below given steps:-
Insert the command button and rename it as “Letter Print”, and then assign the below mentioned code:-
Private Sub CommandButton1_Click()
Dim StartrowAs Integer, lastrow As Integer
Dim MsgAs String
Dim TotalrecordsAs String
Dim name As String, Street_AddressAs String, city As String, region As String, country As String, postal As String
Totalrecords = '=counta(Main_Data!A:A)'
Range('L1') = Totalrecords
Dim mydate As Date
Set WRP = Sheets('Report')
mydate = Date
WRP.Range('A9') = mydate
WRP.Range('A9').NumberFormat = '[$-F800]dddd,mmmm,dd,yyyy'
WRP.Range('A9').HorizontalAlignment = xlLeft
Startrow = InputBox('Enter the first record to print.')
lastrow = InputBox('Enter the last record to print.')
If Startrow>lastrow Then
Msg = 'ERROR' &vbCrLf& 'Starting row must be less than last row'
Msgbox Msg, vbCritical, 'ExcelTip'
End If
For i = Startrow To lastrow
name = Sheets('Main_data').Cells(i, 1)
Street_Address = Sheets('Main_data').Cells(i, 2)
city = Sheets('Main_data').Cells(i, 3)
region = Sheets('Main_data').Cells(i, 4)
country = Sheets('Main_data').Cells(i, 5)
postal = Sheets('Main_data').Cells(i, 6)
Sheets('Report').Range('A7') = name &vbCrLf&Street_Address&vbCrLf& city & region & country &vbCrLf& postal
Sheets('Report').Range('A11') = 'Dear' & ' ' & name & ','
CheckBox1 = True
If CheckBox1 Then
ActiveSheet.PrintPreview
Else
ActiveSheet.PrintOut
Excel Mail Merge Envelopes
End If
Next i
End Sub
Code Explanation: - First, we will define the variables then we will define the date and date format, then we will define the last row and start row. Then we have created message box for transmitting the message. Then we will define the data and range that we want to capture in letter.
- To run the code, press key F5 on the keyboard.
- Then you have to enter first record point. After that, you will get new message box to enter the last record of point.
- And, then you will get the below shown document
- Letter will get updated according to the mentioned details in main data.
This is the way we can automate mail merge through VBA in Microsoft Excel. Images for mac.