welshgasman
Well-known Member
- Joined
- May 25, 2013
- Messages
- 1,402
- Office Version
- 2019
- 2007
- Platform
- Windows
Hi everyone,
I need to create a mailmerge from Excel and use Excel for the mailmerge.
I discovered the code below from Mail Merge in Excel - Microsoft: Office FAQ - Tek-Tips
which works great and I have amended to save each sheet as the mailmerged file.
However it has a drawback where it expects a named range for every header name in the Data sheet.
I have more headers than named ranges required on the Excel form, as I need extra info on the mailmerge, date created, combining several columns etc.
I know how to hide the columns I am not using in the merge, but what would I need to change to only process visible columns?
TIA
I need to create a mailmerge from Excel and use Excel for the mailmerge.
I discovered the code below from Mail Merge in Excel - Microsoft: Office FAQ - Tek-Tips
Code:
Function RangeName(sName As String) As String
RangeName = Application.Substitute(sName, " ", "_")
End Function
Sub MergePrint()
'set up your merge form by naming the merge fields _
with the same name as the data fields you are importing.
'if any data field contains spaces, then substitute an _
UNDERSCORE character for each space in the name.
Dim wsForm As Worksheet, wsData As Worksheet
Dim sRngName As String, r As Long, c As Integer
Set wsForm = Worksheets("My Form") 'change to your sheet name
Set wsData = Worksheets("My Data") 'change to your sheet name
With wsData.Cells(1, 1).CurrentRegion
For r = 2 To .Rows.Count
If Not wsData.Cells(r, 1).EntireRow.Hidden Then
For c = 1 To .Columns.Count
sRngName = wsData.Cells(1, c).Value
Range(RangeName(sRngName)).Value = wsData.Cells(r, c)
Next
wsForm.PrintOut
End If
Next
End With
End Sub
which works great and I have amended to save each sheet as the mailmerged file.
However it has a drawback where it expects a named range for every header name in the Data sheet.
I have more headers than named ranges required on the Excel form, as I need extra info on the mailmerge, date created, combining several columns etc.
I know how to hide the columns I am not using in the merge, but what would I need to change to only process visible columns?
TIA