Add a row to a Word Table

I've created a Word 2010 document with a table.  Users will need to add information to the table (text, drop-down menus and date picker). They will need to add a row to the table and copy all of the control boxes.  I would like to add a button that they can click on to add the row and copy all formatting/controls below the selected row.  Can someone help me with this code?
Tina RobinsonAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
GrahamSkanConnect With a Mentor RetiredCommented:
No that won't work either.

The problem is that when you click on the button, you move the selection from a table row to the button.

A modified macro could ask which row is to be duplicated. However, I think a better solution would be to use a shortcut keystroke and/or add a QAT button to call the macro. Also the macro could be in a code module of the template, instead of the ThisDocument module of the actual document .
If the document is created from a particular template, use that one, otherwise use the Normal template.

Insert a Module using the Insert menu in the VBA IDE and put the code there:
Private Sub AddIntermediateRow()
    Dim rw As Row
    Dim rng As Range
    
    If Selection.Rows.Count > 0 Then
        Set rw = Selection.Rows.Last
        Set rng = rw.Range
        rng.Copy
        rng.Collapse wdCollapseEnd
        rng.Paste
    End If
End Sub

Open in new window

In find Word Options via the File tab or Office button.
Select Customize.
Select Macros in the 'Choose command from' box
Click the Add>> button to add a button to the QAT
Sreparately, you can click the Customise button and navigate to the macro in the Customize keyboard dialogue. You can then specify the shortcut keys to call the macro
0
 
Rob HensonFinance AnalystCommented:
Hi Tina,

Can you upload a sample file so that we can see the items that will need copying?

When in the last cell of a table (bottom row, far right column) pressing the tab key will create a new row. I know that this copies formatting from the row above; I assume it doesn't copy any of the controls as well?

Thanks
Rob
0
 
aikimarkCommented:
Please post a representative sample document
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Tina RobinsonAuthor Commented:
I've attached the file.  Under each semester, users will need to add additional rows as needed. I wanted to add a button that they can click and it will add the row with the controls/formatting.  Thank you for any help you can give me!
0
 
Paul SauvéRetiredCommented:
no file yet!
0
 
aikimarkCommented:
I've attached the file
I don't see any attachment
0
 
Tina RobinsonAuthor Commented:
0
 
GrahamSkanRetiredCommented:
This will add another row the same as the last one
Sub AddRow()
    Dim tbl As Table
    Dim rng As Range
    
    Set tbl = ActiveDocument.Tables(2)
    Set rng = tbl.Rows.Last.Range
    rng.Copy
    rng.Collapse wdCollapseEnd
    rng.Paste
End Sub

Open in new window

0
 
Tina RobinsonAuthor Commented:
Thank you - I'm new at this and am not sure where to add this.  Do I create an Active X control button and add this when I click on View Code?
0
 
GrahamSkanRetiredCommented:
Oops. I see that you want to copy the selected row.
Sub AddIntermediateRow()
    Dim rw As Row
    Dim rng As Range
    
    Set rw = Selection.Rows.Last
    Set rng = rw.Range
    rng.Copy
    rng.Collapse wdCollapseEnd
    rng.Paste
End Sub

Open in new window

0
 
Tina RobinsonAuthor Commented:
How do I add this code to the button?
0
 
GrahamSkanRetiredCommented:
I thought you had that sussed.

There are several ways, but the simplest would be to add a Macrobutton field. First write your macro, then navigate thus:  Insert tab, Text group, Quick Parts button, Field... item. Choose the Macrobutton field and then browse to your macro. Change the 'Displayed text' to whatever you need and click OK.
0
 
Tina RobinsonAuthor Commented:
I clicked on my control button, view code, add added the above code.  However, when I click on the button (and my cursor is in a table cell) a new row is not added.  What am I doing wrong?  Thanks
0
 
Tina RobinsonAuthor Commented:
GrahamSkan - I don't understand your instructions. Is there anyway you can tell me how to do this with the code that you provided me?
0
 
GrahamSkanRetiredCommented:
Open the VBA editor with Alt + F11.

Select the ThisDocument module of your document's project in Project explorer. Select CommandButton1 in the left-hand list box in the code pane. Select the Click event in the right-hand box. This should open an empty event Sub:
Option Explicit

Private Sub CommandButton1_Click()

End Sub

Open in new window


Put the code into that procedure.
0
 
Tina RobinsonAuthor Commented:
This is what I have:

Private Sub CommandButton1_Click()
Sub AddIntermediateRow()
    Dim rw As Row
    Dim rng As Range
   
    Set rw = Selection.Rows.Last
    Set rng = rw.Range
    rng.Copy
    rng.Collapse wdCollapseEnd
    rng.Paste
End Sub
End Sub

When I click on the button and run it - the first row (Private Sub... is yellow)
0
 
aikimarkCommented:
You can't nest routines like that.  Please change to this:
Private Sub CommandButton1_Click()
    Dim rw As Row
    Dim rng As Range
    
    Set rw = Selection.Rows.Last
    Set rng = rw.Range
    rng.Copy
    rng.Collapse wdCollapseEnd
    rng.Paste
End Sub

Open in new window

0
 
Tina RobinsonAuthor Commented:
Now when I run the macro (after I click on one of the rows) I get the following error message:

Run time error "5941" - The requested member of the collection does not exist.

 I can't believe this is so difficult. I did it in two minutes in Excel but just can't get it to work in Word.  Thanks again for all of your help!!!
0
 
GrahamSkanRetiredCommented:
aikimark, thank you.
I didn't realise that my explanation could be read like that.

Tina Robinson
In case the user's selection is not within a table row, try this.
Private Sub CommandButton1_Click()
    Dim rw As Row
    Dim rng As Range
    
    If  Selection.Rows.Count > 0 then
        Set rw = Selection.Rows.Last
        Set rng = rw.Range
        rng.Copy
        rng.Collapse wdCollapseEnd
        rng.Paste
    endif
End Sub

Open in new window

0
 
Tina RobinsonAuthor Commented:
I just tried that and it's still giving me the same error message.  I think I'm going to give up.
0
 
GrahamSkanRetiredCommented:
Yes I know. I am working on a solution.
0
 
Tina RobinsonAuthor Commented:
I really do appreciate all of your help!
0
 
Tina RobinsonAuthor Commented:
Thank you for all of your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.