h1

Word: Macro to add a value at the beginning of each row of a table

May 2, 2017

I had a strange request — someone had a really long table in Microsoft Word (some 50+ pages long…) and wanted to add an asterisk (star) in front of the first character in each row, no matter how many rows and columns there were in the table or what the original first character in each row was (some first characters were numerals, but most were upper or lower case letters). Doing this manually was going to take hours, and he had several tables across various documents that he wanted to do this to.

My first thought was a find and replace with wildcards routine, but I quickly figured out that I didn’t have enough knowledge to do that, even if it was possible. My next thought was to add a new first column, populate it with asterisks, then merge the first and second columns of each row — the problem was that merging required selecting each pair of 1st and 2nd column cells for EACH row, then merging, then removing the added paragraph mark created by the merge process. That was going to take as long or even longer than adding the asterisks manually!

So I contacted my friend Dave Gash (www.davegash.com), a US-based programmer/tech writer, to see if he could help. His forte isn’t VBA, but he does know programming logic (and he knows what to search for), and by the next morning he had a solution for me. Actually he had a solution that was TOO good — his first solution (Macro 2 below) added the asterisk to the beginning of all rows in ALL tables in the document. But that’s not what my client wanted — he wanted to add asterisks to all rows in a specific table, not every table. A bit of tweaking and Dave came back a few minutes later with the solution that suited my client (Macro 1 below). When I ran it in test mode, the changes were almost instant, but they took about 5 mins on the 50+ page table! That was still many hours less than it would’ve taken manually. I owe Dave a beer or three!

Macro 1: Add an asterisk to the beginning of all rows in a specific table

Sub AddAsteriskToTableRow()
   ' From Dave Gash www.davegash.com April 2017 '
   ' Collapse the range to start so as to not have to deal with '
   ' multi-segment ranges. Then check if cursor is within a table. '

   Selection.Collapse Direction:=wdCollapseStart
   If Not Selection.Information(wdWithInTable) Then
      MsgBox "You can only run this when your cursor is within a table."
      Exit Sub
   End If

   ' Process every row in the current table. '
   Dim row As Integer
   Dim rng As Range

   For row = 1 To Selection.Tables(1).Rows.Count
      ' Get the range for the leftmost cell. '
      Set rng = Selection.Tables(1).Rows(row).Cells(1).Range

      ' For each, insert asterisk in leftmost cell.'
      ' Change value in quote marks if you want something other than an asterisk. '
      rng.InsertBefore ("*")
   Next
End Sub

Macro 2: Add an asterisk to the beginning of all rows in ALL tables in a document

Sub AddAsterisksToAllTables()
    
' From Dave Gash www.davegash.com April 2017
' Loop through all tables
For tbl = 1 To ActiveDocument.Tables.Count
 
    'Set up row and range vars
    Dim row As Integer
    Dim rng As Range
 
    'Loop through rows in current table
    For row = 1 To ActiveDocument.Tables(tbl).Rows.Count
        ' Get the range for the leftmost (column 1) cell
        Set rng = ActiveDocument.Tables(tbl).Rows(row).Cells(1).Range
        ' Insert the asterisk before the text in leftmost cell
        ' Change value in quote marks if you want something
        ' other than an asterisk.
        rng.InsertBefore ("*") 
    Next row 
Next tbl 
End Sub

Notes

  • The examples above use an asterisk as the added character, but you can make that character anything you want — one or more letters or numbers or standard punctuation symbols (or a combination), with or without a space. Just change the value in the double-quotation marks in the macro to what you want (e.g. you might have a list of product numbers that need to have ‘MQ’ added in front of them — change “*” to “MQ” [or “MQ-” or “MQ “]). You might want to change the name of the macro too, to reflect what you’re adding (e.g. AddMQToTableRow).
  • Don’t run this twice in the same table, otherwise you’ll end up with two asterisks!
  • Dave asked me to add this acknowledgement: ‘My source was from a StackOverflow (the coder’s friend, believe me!) member (apparently also from Western Australia!). … I modified the code I found there for your purposes.’ Source: http://stackoverflow.com/questions/7226721/how-can-you-get-the-current-table-in-ms-word-vba

[Links last checked May 2017]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: