Add rows to table

B

bryan

This is a template -
I want to be able to add rows to a table (7 colums).
Below is example of table:
Each <> is a form field
The Net1 is a formula of GAmt1 - (GAmt1 * Rate1)
By clicking the macroButton field <insert row above> a new row would be
added above and the form fields to be named 'Pol2' , 'Name2', etc.
Net2 would be formula same as for Net1.
Next added row would have 'Pol3', Name3', etc.
The field after the text 'Total' be a formula of adding the Net Amounts
(Net1 + Net2 + .....) for as many rows that are added.
Also would like the macroButton to be hidden so it does not save or print.

______________________________________________________________
|<Pol1> | <Name1>| <Desc1>| <Date1>| <GAmt1>| <Rate1> | <Net1> |
______________________________________________________________
| 'insert row above' Total:
| <TNet> |
______________________________________________________________


Thanks in advance for your help,
Bryan
 
B

bryan

Format issue - table should look like:
|<Pol1> | <Name1>| <Desc1>| <Date1>| <GAmt1>| <Rate1> | <Net1> |
______________________________________________________________
| 'insert row above' Total: | <TNet> |
______________________________________________________________
 
D

Doug Robbins - Word MVP

The following series of macros that are taken from one of my forms do not do
exactly what you want, but should show you how to get started:

Sub AddRow()
Dim NewRow As Row
Dim FF As FormField
Dim Response
Response = MsgBox("Do you want to add another row?", vbYesNo + vbQuestion,
"Previous Change Orders")
If Response = vbYes Then
With ActiveDocument
.Unprotect
Set NewRow = .Bookmarks("History").Range.Tables(1).Rows.Add
Set FF = .FormFields.Add(NewRow.Cells(1).Range,
wdFieldFormTextInput)
Set FF = Nothing
Set FF = .FormFields.Add(NewRow.Cells(2).Range,
wdFieldFormTextInput)
Set FF = Nothing
Set FF = .FormFields.Add(NewRow.Cells(3).Range,
wdFieldFormTextInput)
Set FF = Nothing
Set FF = .FormFields.Add(NewRow.Cells(4).Range,
wdFieldFormTextInput)
Set FF = Nothing
NewRow.Cells(3).Range.FormFields(1).ExitMacro = "FormatCurrency"
NewRow.Cells(4).Range.FormFields(1).ExitMacro = "AddRow"
NewRow.Cells(5).Range.Text = "Days"
Set FF = Nothing
.Protect wdAllowOnlyFormFields, NoReset
With .Bookmarks("History").Range.Tables(1)
If .Rows.Count > 2 Then
.Cell(.Rows.Count, 1).Range.FormFields(1).Result =
..Cell(.Rows.Count - 1, 1).Range.FormFields(1).Result + 1
Else
.Cell(.Rows.Count, 1).Range.FormFields(1).Result = 2
End If
.Cell(.Rows.Count, 2).Range.FormFields(1).Select
.Cell(.Rows.Count - 1, 4).Range.FormFields(1).ExitMacro =
"CalculateNewDate"
End With
End With
End If
CalculateNewDate
End Sub
Sub FormatCurrency()
Dim i As Long
With ActiveDocument.Bookmarks("history").Range.Tables(1)
For i = 1 To .Rows.Count
With .Cell(i, 3).Range.FormFields(1)
.Result = Format(.Result, "#,##0.00")
End With
Next i
End With
CalculateNewValue
End Sub
Sub CalculateNewValue()
Dim i As Long
Dim PreviousChanges As Double
With ActiveDocument
PreviousChanges = 0
With .Bookmarks("History").Range.Tables(1)
For i = 1 To .Rows.Count
PreviousChanges = PreviousChanges + Val(Replace(.Cell(i,
3).Range.FormFields(1).Result, ",", ""))
Next i
End With
.FormFields("NewValue").Result =
Format(Val(Replace(.FormFields("value1").Result, ",", "")) +
Val(Replace(.FormFields("value2").Result, ",", "")) + PreviousChanges,
"#,##0.00")
End With
End Sub

In the form in which those macros are used, the equivalent of your:

|<Pol1> | <Name1>| <Desc1>| <Date1>| <GAmt1>| <Rate1> | <Net1> |

are in a separate table to which (in that case) the bookmark name History is
applied and the AddRow macro is set to be run on exit from the last
formfield in the last row of that table for which the user provides input
(<Rate1> in your case.

The macro does not bother assigning bookmark names to the formfields in the
added row and rather performs the calculation by referencing the first
formfield in the .Range of the cells in the row for which the calculation is
being made.

Send me an email message if you want a copy of this form to see how it all
works.


--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 
B

bryan

Hi Doug,
Could you send me a copy of your form to (e-mail address removed), would be much
appreciated.
I was looking to use formfields vs. Inputboxes and a MacroButton field but,
this could work.
How would I add a row (assuming they enter 'No' to adding a row) which would
then give me --- Total Net: <TNet> where TNet would be a sum of the Nets

I am also confused on your code trying to find a calculation for <Net> of
each row

Thanks,
Bryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top