Q: I have attached an Excel workbook containing a list of 1,855 names in columnar format (see a partial example workbook pictured below), and I want to rearrange them into mailing label format—that is, I want all of the names and addresses in a single column.
I have successfully written a macro that rearranges a single row of data into the desired format and then moves the cursor to the next row. However, I don’t want to have to rerun the macro 1,854 times manually. How do I make this macro repeat itself automatically?
A: I believe you would be better off using Word’s Mail Merge tool to accomplish your goal, but because your question offers a good opportunity to discuss macro loops, let us proceed using your macro-based approach. I tested your macro, and it works well; you just need to add some additional code to make it repeat 1,854 times. To do this, from Excel’s View tab, select Macros, View Macros, and then select your macro in the Macro name dropdown box and click the Edit button, as pictured below.
This action will display your macro text. Next, immediately above the macro command lines, insert two lines of code: Let x = 0 and Do While x < 1855, as pictured below. In addition, immediately below the macro command lines, insert these additional two lines of code: x = x + 1 and Loop (pictured below).
When completed, select File, Save,
and thereafter your macro will repeat the number of times specified in
the Do While line command. You can download an example of this
workbook at carltoncollins.com/loopy.xlsm.
J. Carlton Collins ( firstname.lastname@example.org ) is a technology consultant, CPE instructor, and a JofA contributing editor.
Submit a question
Do you have technology questions for this column? Or, after reading an answer, do you have a better solution? Send them to email@example.com. We regret being unable to individually answer all submitted questions.