Forum Discussion
Mail merge nested if statements
In what application is the data? If you have it in an Excel Workbook and you run a macro containing the following code
Dim shtsource As Worksheet
Dim shttarget As Worksheet
Dim c As Long, r As Long, rowtarget As Long, c1 As Long
Set shtsource = Sheets(1)
Set shttarget = Sheets.Add
For c = 1 To 5
shttarget.Range("A1").Offset(0, c - 1) = shtsource.Range("A1").Offset(0, c - 1)
Next c
shttarget.Range("A1").Offset(0, 5) = "Wish Item"
rowtarget = 1
With shtsource.Range("A1")
For r = 1 To .CurrentRegion.Rows.Count - 1
For c = 1 To 6
shttarget.Range("A1").Offset(rowtarget, c - 1) = .Offset(r, c - 1)
Next c
rowtarget = rowtarget + 1
For c1 = 7 To 12
If .Offset(r, c1 - 1) <> "" Then
For c = 1 To 5
shttarget.Range("A1").Offset(rowtarget, c - 1) = .Offset(r, c - 1)
Next c
shttarget.Range("A1").Offset(rowtarget, 5) = .Offset(r, c1 - 1)
rowtarget = rowtarget + 1
End If
Next c1
Next r
End With
It will add a sheet to the workbook with the data re-arranged as shown below which can then be used as the data source for a straight forward Label type mail merge
First Name | Family # | Source | Age | Gender | Wish Item |
Jaxson W. | 7 | BLUE BUTTERFLY | 8 | Male | Hexbugs |
Jaxson W. | 7 | BLUE BUTTERFLY | 8 | Male | Legos |
Odette | 8 | BLUE BUTTERFLY | 9 | Female | Mote Marina Tickets |
Odette | 8 | BLUE BUTTERFLY | 9 | Female | Amazon gift card |
Odette | 8 | BLUE BUTTERFLY | 9 | Female | Hatchimals |
Amelie | 8 | BLUE BUTTERFLY | 12 | Female | Amazon gift card |
Amelie | 8 | BLUE BUTTERFLY | 12 | Female | Mini golf gift card |
Amelie | 8 | BLUE BUTTERFLY | 12 | Female | The Hobbit series |
Aaliyah H. | 9 | BLUE BUTTERFLY | 8 | Female | New shoes (skating shoes) |
Aaliyah H. | 9 | BLUE BUTTERFLY | 8 | Female | Magna books (anime) |
Aaliyah H. | 9 | BLUE BUTTERFLY | 8 | Female | Black hoodie |
Haley W. | 10 | BLUE BUTTERFLY | 11 | Female | Anything with The Beatles |
Haley W. | 10 | BLUE BUTTERFLY | 11 | Female | Roblox gift card |
Haley W. | 10 | BLUE BUTTERFLY | 11 | Female | Drawing supplies |
Michael G. | 11 | BLUE BUTTERFLY | 5 | Male | Ryan's World Shadow Warrior Mystery Box |
Michael G. | 11 | BLUE BUTTERFLY | 5 | Male | Ryan's World Royal Treasure Chest |
Michael G. | 11 | BLUE BUTTERFLY | 5 | Male | Just Dance Switch 2021 |
Lily G. | 11 | BLUE BUTTERFLY | 9 | Female | "Harry Potter Lego--4 Privet Drive |
Lily G. | 11 | BLUE BUTTERFLY | 9 | Female | #75968" |
Lily G. | 11 | BLUE BUTTERFLY | 9 | Female | Lego Hidden Side--#70432 |
Lily G. | 11 | BLUE BUTTERFLY | 9 | Female | Electric scooter |
Jonathan G. | 11 | BLUE BUTTERFLY | 13 | Male | Playstation Gift Card |
Jonathan G. | 11 | BLUE BUTTERFLY | 13 | Male | Nike Basketball Shorts size Large |
Jonathan G. | 11 | BLUE BUTTERFLY | 13 | Male | Tampa Buc's T Shirt Size Large |
Jonathan G. | 11 | BLUE BUTTERFLY | 13 | Male | Nike TShirt Size Large |
Summayah | 12 | BLUE BUTTERFLY | 8 | Female | Baby doll |
Summayah | 12 | BLUE BUTTERFLY | 8 | Female | Baby doll carrier |
Summayah | 12 | BLUE BUTTERFLY | 8 | Female | Barbies |
Keplar | 13 | BLUE BUTTERFLY | 11 | Male | Stickbots |
Keplar | 13 | BLUE BUTTERFLY | 11 | Male | Legos |
Keplar | 13 | BLUE BUTTERFLY | 11 | Male | The Far Side Comics |
Tasman | 13 | BLUE BUTTERFLY | 13 | Female | Loopity--loops yarn to make blankets (any color) |
Tasman | 13 | BLUE BUTTERFLY | 13 | Female | Bath bombs/face masks (for sensitive skin) |
Tasman | 13 | BLUE BUTTERFLY | 13 | Female | Cat necklace/earrings/bracelet |