PDA

View Full Version : Solved: increment bootkmarks in generated rows...



floorfiller
12-15-2012, 01:05 AM
Hey guys,

I'm really new to VBA so please bare with me.

I'm working on a little project for work and I think I'm about halfway there. What I'm trying to accomplish is this:

I have a 2 column table and am generating additional rows in it. In the left most cell I just have basic text as a label, so I don't really care about increments for it.

however, in the right cell I am going to have a dollar amount that I would like to total using the form field calculation. In order to do this, I have to increment the bookmark and add that new bookmark to the calculation field. I think this is pretty easy to do, I just don't know the skills to make it happen.

here is a look at what i have so far:


Sub Macro1()
'
'add row at bottom of table
Selection.InsertRowsBelow 1
Selection.FormFields.add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.PreviousField.Select
With Selection.FormFields(1)
.Name = "label"
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.OwnHelp = False
.HelpText = ""
.OwnStatus = False
.StatusText = ""
With .TextInput
.EditType Type:=wdRegularText, Default:="", Format:=""
.Width = 0
End With
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.PreviousField.Select
With Selection.FormFields(1)
.Name = "charge"
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.OwnHelp = False
.HelpText = ""
.OwnStatus = False
.StatusText = ""
With .TextInput
.EditType Type:=wdNumberText, Default:="", Format:= _
"$#,##0.00;($#,##0.00)"
.Width = 0
End With
End With

End Sub


so the above works how i need it to, however i'm not sure of the syntax to increment the bookmark name with every new generation of a row. i assume i need to do some sort of static variable, but again i'm just not familiar enough to make it work.

any help is certainly appreciated :)

macropod
12-15-2012, 02:48 AM
If you're summing a column that doesn't have any empty cells in the column, you can use a simple formula field with the "=SUM(ABOVE)" formula. For example:
Sub AddRow()
Dim Rng As Range
With Selection.Tables(1).Range
.Rows.Add
Set Rng = .Rows.Last.Cells(2).Range
Rng.Text = vbNullString
Rng.Collapse
.FormFields.Add Range:=Rng, Type:=wdFieldFormTextInput
With Rng.FormFields(1)
.TextInput.EditType Type:=wdNumberText, Default:="0", Format:="$#,##0.00;($#,##0.00)"
.CalculateOnExit = True
End With
Set Rng = .Rows.Last.Cells(1).Range
Rng.Text = vbNullString
Rng.Collapse
.FormFields.Add Range:=Rng, Type:=wdFieldFormTextInput
.MoveEnd wdTable, 1
Set Rng = .Rows.Last.Cells(2).Range
Rng.Text = vbNullString
Rng.Collapse
.Fields.Add Range:=Rng, Type:=wdFieldEmpty, PreserveFormatting:=False, _
Text:="=SUM(ABOVE) \#" & Chr(34) & "$,0.00;($,0.00)" & Chr(34)
End With
End Sub

floorfiller
12-15-2012, 09:08 AM
beautiful. thanks for your help man. that's much easier. i've got it working how i want now. much appreciated :)