Issue a letter for every article in a column

Hansulet

Board Regular
Joined
Jan 24, 2013
Messages
157
Office Version
  1. 2021
Platform
  1. Windows
I have a large data base including over 20 columns (with Place, Name, Personal Code / Insurance Number) and 30000 rows.
For every article in column A, I have to print a letter including rows associated with respective Place.

I tried in MSWord with MailMergeDirectory but I fail.
I hope in Excel the result will be good.

Below, I put an example:

col A col B col C colD col E .............col AM
Place1 Name1 CNP1
Place1 Name2 CNP2
Place1 Name3 CNP3
Place2 Name4 CNP4
Place2 Name5 CNP5
Place2 Name6 CNP6
Place2 Name7 CNP7
Place3 Name8 CNP8
Place3 Name9 CNP9
__________________________________________________________________________________

The letter for Place 1 must be as follows:

Place1

Name1 CNP1
Name2 CNP2
Name3 CNP3
__________________________________________________________________________
The letter for Place 2 must be as follows:

Place2


Name4 CNP4
Name5 CNP5
Name6 CNP6
Name7 CNP7
____________________________________________________________________
The letter for Place 3 must be as follows:

Place3


Name8 CNP8
Name9 CNP9
______________________________________________________________________
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
The letter for Place 1 must be as follows:

Place1

Name1 CNP1
Name2 CNP2
Name3 CNP3
__________________________________________________________________________
The letter for Place 2 must be as follows:

Place2


Name4 CNP4
Name5 CNP5
Name6 CNP6
Name7 CNP7
____________________________________________________________________
The letter for Place 3 must be as follows:

Place3


Name8 CNP8
Name9 CNP9
______________________________________________________________________
1. Where should these results be placed?

2. Are columns D:AM relevant for (ie included in) the results, or can they be ignored?

3. Does the existing data have headings in row 1?
 
Upvote 0
The best tool for doing what you wish is MailMerge in Word. To get this functionality in Excel would take a bit of work. I would suggest that you go back to Word and study the MailMerge videos that are readily available on the internet.
 
Upvote 0
Hi,

You could create a Pivot Table from your data, set the Print Area, Set Page Breaks for each Item and you should get each item printed out on a seperate sheet.

Sample Data...

Excel Workbook
ABCD
1PlaceNamePersonal Code / Insurance Number
2Place1Name1CNP1
3Place1Name2CNP2
4Place1Name3CNP3
5Place2Name4CNP4
6Place2Name5CNP5
7Place2Name6CNP6
8Place2Name7CNP7
9Place3Name8CNP8
10Place3Name9CNP9
11
Sheet7



Sample Pivot Table....

Excel Workbook
ABC
1
2
3
4PlaceNamePersonal Code / Insurance Number
5Place1Name1CNP1
6Name2CNP2
7Name3CNP3
8Place2Name4CNP4
9Name5CNP5
10Name6CNP6
11Name7CNP7
12Place3Name8CNP8
13Name9CNP9
14
15
Sheet8


I hope that helps.

Ak
 
Upvote 0
Sorry. Inadvertently<small></small>, I fail to mention your request information:

The result will be placed in a different sheet.
The columns D:AM are not relevant. These columns can be ignored.
The data base have a heading in row 1
 
Upvote 0
Sorry. Inadvertently<small></small>, I fail to mention your request information:

The result will be placed in a different sheet.
The columns D:AM are not relevant. These columns can be ignored.
The data base have a heading in row 1
Try this in a copy of your workbook.

My original data is in a sheet called "Data". Change the code to suit your sheet name.
Code:
Sub Test()
  Dim c As Range, f As Range
  
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  Sheets("Data").Copy After:=Sheets("Data")
  With ActiveSheet
    .Name = "Result"
    .Range("A1").CurrentRegion.Subtotal GroupBy:=1, Function:=xlCount, TotalList:=Array(2), _
        Replace:=True, PageBreaks:=False, SummaryBelowData:=False
    .Rows("1:2").Delete
    Set f = .Columns("B").SpecialCells(xlFormulas)
    For Each c In f
      c.Value = c.Offset(1, -1).Value
    Next c
    f.EntireRow.Insert
    f.Offset(1).EntireRow.Insert
    .UsedRange.RemoveSubtotal
    .Columns("A").Delete
  End With
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub

My original sheet:

Excel Workbook
ABC
1col Acol Bcol C
2Place1Name1CNP1
3Place1Name2CNP2
4Place1Name3CNP3
5Place2Name4CNP4
6Place2Name5CNP5
7Place2Name6CNP6
8Place2Name7CNP7
9Place3Name8CNP8
10Place3Name9CNP9
11Place4Name10CNP10
12Place5Name11CNP11
13Place5Name12CNP12
14
Data




My results:

Excel Workbook
ABC
1
2Place1
3
4Name1CNP1
5Name2CNP2
6Name3CNP3
7
8Place2
9
10Name4CNP4
11Name5CNP5
12Name6CNP6
13Name7CNP7
14
15Place3
16
17Name8CNP8
18Name9CNP9
19
20Place4
21
22Name10CNP10
23
24Place5
25
26Name11CNP11
27Name12CNP12
28
29
Result
 
Upvote 0
Thanks a lot, Peter.
Your code is very helpful for me.
For each place, together with associated rows, can be inserted a sheet renamed as place name?
 
Upvote 0
Thanks a lot, Peter.
Your code is very helpful for me.
For each place, together with associated rows, can be inserted a sheet renamed as place name?
See if this modification is okay.
Code:
Sub Test2()
  Dim c As Range, f As Range
  
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  Sheets("Data").Copy After:=Sheets("Data")
  With ActiveSheet
    .Name = "Result"
    .Range("A1").CurrentRegion.Subtotal GroupBy:=1, Function:=xlCount, TotalList:=Array(2), _
        Replace:=True, PageBreaks:=False, SummaryBelowData:=False
    .Rows("1:2").Delete
    Set f = .Columns("B").SpecialCells(xlFormulas)
    For Each c In f
      c.Value = c.Offset(1, -1).Value
    Next c
    f.EntireRow.Insert
    .UsedRange.RemoveSubtotal
    .Columns("A").Delete
    For Each c In .Columns("A").SpecialCells(xlConstants).Areas
      Sheets.Add After:=Sheets(Sheets.Count)
      With ActiveSheet
        .Range("A1").Resize(c.Rows.Count, 2).Value = c.Resize(, 2).Value
        On Error Resume Next
        .Name = .Range("A1").Value
        On Error GoTo 0
      End With
    Next c
  End With
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
or:
Code:
Sub M_snb()
  sn = Sheets("sheet1").Cells(1).CurrentRegion

  For j = 1 To UBound(sn) - 1
    c01 = c01 & "," & j
    If sn(j, 1) <> sn(j + 1, 1) Then
      Sheets.Add.Name = sn(j, 1)
      Sheets(sn(j, 1)).Cells(1).Resize(UBound(Split(c01, ",")), 2) = Application.Index(sn, Application.Transpose(Split(Mid(c01, 2), ",")), Array(2, 3))
      c01 = ""
    End If
  Next

  c01 = c01 & "," & j
  Sheets.Add.Name = sn(j, 1)
  Sheets(sn(j, 1)).Cells(1).Resize(UBound(Split(c01, ",")), 2) = Application.Index(sn, Application.Transpose(Split(Mid(c01, 2), ",")), Array(2, 3))
End Sub
 
Upvote 0

Forum statistics

Threads
1,207,423
Messages
6,078,443
Members
446,338
Latest member
AliB

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top