Calculations Form -- Help!

J

jasonbarker

I'm having a speed issue that I think could be eliminated with a littl
more knowledge.

I am creating a template form in Microsoft Word. It will be a deposi
ticket with 28 dollar amounts. Each dollar amount is split into tw
text form fields (one for the dollar portion of the amount and th
other for the cents portion of the dollar amount). I have set the nam
of each field (DollarAmt_01 to DollarAmt_28 and CentsAmt_01 t
CentsAmt_28). I have also created a macro that gets run when the focu
exits the text form field for both the dollar and cents fields. Thi
macro cycles through each dollar amount adding them up. Then it cycle
through the cents amount, adding them up, multiplying them by 0.01 an
adding the result to the dollar amount total.

Here's a snippet of my code for the macro:
Sub CalcDepositTicketTotals()
Dim dollarTotal, centsTotal As Double

dollarTotal = 0
dollarTotal = dollarTotal
Val(ActiveDocument.FormFields("DollarAmt_01").Result)
dollarTotal = dollarTotal
Val(ActiveDocument.FormFields("DollarAmt_02").Result)
...
dollarTotal = dollarTotal
Val(ActiveDocument.FormFields("DollarAmt_28").Result)

centsTotal = 0
centsTotal = centsTotal
Val(ActiveDocument.FormFields("CentsAmt_01").Result)
centsTotal = centsTotal
Val(ActiveDocument.FormFields("CentsAmt_02").Result)
...
centsTotal = centsTotal
Val(ActiveDocument.FormFields("CentsAmt_28").Result)

centsTotal = centsTotal * 0.01
dollarTotal = dollarTotal + centsTotal

' US Dollar Amount
ActiveDocument.FormFields("USD_Total").Result = Str(dollarTotal)

End Sub
=====================

I'm sure that I am going about this the wrong way because this i
taking way too long to process (about 16-18 seconds). Certainly, yo
can tell that I am not experienced with programming in Word. An
suggestions on what I can do to speed things up?

Thanks!
Jaso
 
P

Perry

Wud something like below code sequence speed the calculation up?

Dim f As FormField
Dim d As Double 'dollar subtotal
Dim c As Double 'cents subtotal

'loop through all formfields
For Each f In ActiveDocument.FormFields
'criterium 1st 3 digits of formfield name
Select Case Left(f.Name, 3)
Case "Dol"
d = d + Val(f.Result)
Case "Cen"
c = c + Val(f.Result)
Case Else
End Select
Next

c = c / 100
d = d + c
ActiveDocument.FormFields("USD_Total").Result = d

Krgrds,
Perry
 
J

jasonbarker

Is there an easier way to loop through form fields? Perhaps with a FO
loop? How could this be accomplished
 
P

Perry

Is there an easier way to loop through form fields? Perhaps with a FOR
loop? How could this be accomplished?

You didn't happen to see the for next loop in my previous msg?
I've copied the contents of which between <q> & <unq>

Krgrds,
Perry

<q>
Dim f As FormField
Dim d As Double 'dollar subtotal
Dim c As Double 'cents subtotal

'loop through all formfields
For Each f In ActiveDocument.FormFields
'criterium 1st 3 digits of formfield name
Select Case Left(f.Name, 3)
Case "Dol"
d = d + Val(f.Result)
Case "Cen"
c = c + Val(f.Result)
Case Else
End Select
Next

c = c / 100
d = d + c
ActiveDocument.FormFields("USD_Total").Result = d
<unq>
 

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