Excel Mail Merge Macro: Write to Word Document loop issue

poutineking

New Member
Joined
Jan 29, 2019
Messages
1
I have data set that contains ID numbers on Column A that repeat multiple times and vary from ID # to ID #. For example ID # 123 may take up 17 rows, while ID # 456 may take up 5 rows.

To add complexity, within each ID #'s rows, there are two types of data they can fall under (i.e. ID # 123 (from above example) - 10 of those rows go under "Table 1" in Word Document and the other 7 rows go under "Table 2". I have a Column (Column B) in the Excel data that identifies which Table in Word that row is supposed to go under.

Excel data is sorted by Column A, then by Column B so that all ID #'s and all table data are next to each other.

I have successfully been able to write to Word and the macro distinguish between putting data between Table 1 or 2, but only do this for one single ID #'s data (i.e. Excel wrote to Word for ID # 123 and placed 10 rows on "Table 1" and 7 rows on "Table 2") and save the Word Document as ID # 123.

What I am having trouble with is then creating a new file for ID # 456 and restarting the distingushing process between data going to "Table 1"or "Table 2".

If I run a while ID # is the same loop the macro creates 17 Word Documents for ID # 123, and 5 Word documents for ID # 456. How can I make the macro wirte two Word Documents, rather than 22?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You could use a normal letter merge in conjunction with two DATABASE fields - one for each table - and a macro to drive the process. An outline of this approach can be found at: http://answers.microsoft.com/en-us/...g-tables/8bce1798-fbe8-41f9-a121-1996c14dca5d
Conversely, if you're using an Excel workbook with a separate table with just a single instance of each of the grouping criteria, a DATABASE field in a normal ‘letter’ mailmerge main document could be used without the need for a macro. An outline of this approach can be found at:
https://answers.microsoft.com/en-us...gle-page/4edb4654-27e0-47d2-bd5f-8642e46fa103
For a working example, see:
http://www.msofficeforums.com/mail-merge/37844-mail-merge-using-one-excel-file-multiple.html

The DATABASE field can even be used without recourse to a mailmerge. An example of such usage can be found at: http://www.msofficeforums.com/mail-merge/21847-mail-merge-into-different-coloumns.html#post67097
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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