- I have a table in Word 2000, 6 rows, 2 columns.
- In the first column, rows 1-5 are for review criteria, and in the
second column is a dropdown box in each cell with rating values from
0-4.
- The user selects a rating from the dropdown box, then in the last
cell of the table (row 5, col 2), a formula calculate the average.

The problem is, not all of the fields are mandatory, so the number of
criteria can range from 1 to 5. Also, it is possible to get a "0.0"
rating.

I had been using this; obviously, my question involves how to make the
value of i dynamic based on the number of criteria used:

Sub QTotal()

Dim aa As Single, i As Single

aa = 0
For i = 1 To 5
aa = aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
Next i
aa = aa / 5

ActiveDocument.FormFields("QTotal").Result = _
Format(aa, "#.##")

End Sub

Thank you,

Chris

Re: Calculating in VBA with changing criteria by Karen

Karen
Tue Sep 12 14:20:35 CDT 2006

Hiya TomorrowsMan,

You could check each field first to see if it is empty or using another
variable (j), test for an empty field, increment 'j' and use it as your
divisor. With your current code you could try this:

Sub QTotal()

Dim aa As Single, i As Single, j As Single

aa = 0
j = 0
For i = 1 To 5
if Val(ActiveDocument.FormFields("Qt0" & i).Result) <> "" Then
aa = aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
j = j + 1
end if
Next i
aa = aa / j

ActiveDocument.FormFields("QTotal").Result = _
Format(aa, "#.##")

End Sub


Karen

"TomorrowsMan" <tomorrowsman@gmail.com> wrote in message
news:1158088263.759420.45110@i42g2000cwa.googlegroups.com...
- I have a table in Word 2000, 6 rows, 2 columns.
- In the first column, rows 1-5 are for review criteria, and in the
second column is a dropdown box in each cell with rating values from
0-4.
- The user selects a rating from the dropdown box, then in the last
cell of the table (row 5, col 2), a formula calculate the average.

The problem is, not all of the fields are mandatory, so the number of
criteria can range from 1 to 5. Also, it is possible to get a "0.0"
rating.

I had been using this; obviously, my question involves how to make the
value of i dynamic based on the number of criteria used:

Sub QTotal()

Dim aa As Single, i As Single

aa = 0
For i = 1 To 5
aa = aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
Next i
aa = aa / 5

ActiveDocument.FormFields("QTotal").Result = _
Format(aa, "#.##")

End Sub

Thank you,

Chris



Re: Calculating in VBA with changing criteria by Jay

Jay
Tue Sep 12 14:26:18 CDT 2006

Just be careful of data types... the If statement should be

if Val(ActiveDocument.FormFields("Qt0" & i).Result) <> 0 Then

because Val returns a number, not a string.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Karen wrote:
> Hiya TomorrowsMan,
>
> You could check each field first to see if it is empty or using
> another variable (j), test for an empty field, increment 'j' and use
> it as your divisor. With your current code you could try this:
>
> Sub QTotal()
>
> Dim aa As Single, i As Single, j As Single
>
> aa = 0
> j = 0
> For i = 1 To 5
> if Val(ActiveDocument.FormFields("Qt0" & i).Result) <> "" Then
> aa = aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
> j = j + 1
> end if
> Next i
> aa = aa / j
>
> ActiveDocument.FormFields("QTotal").Result = _
> Format(aa, "#.##")
>
> End Sub
>
>
> Karen
>
> "TomorrowsMan" <tomorrowsman@gmail.com> wrote in message
> news:1158088263.759420.45110@i42g2000cwa.googlegroups.com...
> - I have a table in Word 2000, 6 rows, 2 columns.
> - In the first column, rows 1-5 are for review criteria, and in the
> second column is a dropdown box in each cell with rating values from
> 0-4.
> - The user selects a rating from the dropdown box, then in the last
> cell of the table (row 5, col 2), a formula calculate the average.
>
> The problem is, not all of the fields are mandatory, so the number of
> criteria can range from 1 to 5. Also, it is possible to get a "0.0"
> rating.
>
> I had been using this; obviously, my question involves how to make the
> value of i dynamic based on the number of criteria used:
>
> Sub QTotal()
>
> Dim aa As Single, i As Single
>
> aa = 0
> For i = 1 To 5
> aa = aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
> Next i
> aa = aa / 5
>
> ActiveDocument.FormFields("QTotal").Result = _
> Format(aa, "#.##")
>
> End Sub
>
> Thank you,
>
> Chris



Re: Calculating in VBA with changing criteria by Karen

Karen
Tue Sep 12 14:41:40 CDT 2006

This is a multi-part message in MIME format.

------=_NextPart_000_03D9_01C6D671.2D2F7050
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Yep, should have caught that :)

Karen Hagerman

Faculty

University of Phoenix

kahager@email.uophx.edu=20

khagerman@email.wintu.edu

206-309-0438 (Leave a Message)

"Jay Freedman" <jay.freedman@verizon.net> wrote in message =
news:%23Y4vTFq1GHA.2176@TK2MSFTNGP04.phx.gbl...
Just be careful of data types... the If statement should be

if Val(ActiveDocument.FormFields("Qt0" & i).Result) <> 0 Then

because Val returns a number, not a string.

--=20
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the =
newsgroup so=20
all may benefit.

Karen wrote:
> Hiya TomorrowsMan,
>
> You could check each field first to see if it is empty or using
> another variable (j), test for an empty field, increment 'j' and use
> it as your divisor. With your current code you could try this:
>
> Sub QTotal()
>
> Dim aa As Single, i As Single, j As Single
>
> aa =3D 0
> j =3D 0
> For i =3D 1 To 5
> if Val(ActiveDocument.FormFields("Qt0" & i).Result) <> "" Then
> aa =3D aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
> j =3D j + 1
> end if
> Next i
> aa =3D aa / j
>
> ActiveDocument.FormFields("QTotal").Result =3D _
> Format(aa, "#.##")
>
> End Sub
>
>
> Karen
>
> "TomorrowsMan" <tomorrowsman@gmail.com> wrote in message
> news:1158088263.759420.45110@i42g2000cwa.googlegroups.com...
> - I have a table in Word 2000, 6 rows, 2 columns.
> - In the first column, rows 1-5 are for review criteria, and in the
> second column is a dropdown box in each cell with rating values from
> 0-4.
> - The user selects a rating from the dropdown box, then in the last
> cell of the table (row 5, col 2), a formula calculate the average.
>
> The problem is, not all of the fields are mandatory, so the number =
of
> criteria can range from 1 to 5. Also, it is possible to get a "0.0"
> rating.
>
> I had been using this; obviously, my question involves how to make =
the
> value of i dynamic based on the number of criteria used:
>
> Sub QTotal()
>
> Dim aa As Single, i As Single
>
> aa =3D 0
> For i =3D 1 To 5
> aa =3D aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
> Next i
> aa =3D aa / 5
>
> ActiveDocument.FormFields("QTotal").Result =3D _
> Format(aa, "#.##")
>
> End Sub
>
> Thank you,
>
> Chris=20


------=_NextPart_000_03D9_01C6D671.2D2F7050
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2963" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DVerdana>Yep, should have caught that :)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>
<DIV>
<DIV><STRONG><SPAN style=3D"FONT-WEIGHT: normal; FONT-FAMILY: Verdana">
<DIV>
<DIV class=3DSection1>
<P style=3D"MARGIN: 0in 0in 0pt"><FONT color=3D#008000>Karen =
Hagerman</FONT></P>
<P style=3D"MARGIN: 0in 0in 0pt"><FONT =
color=3D#008000>Faculty</FONT></P>
<P style=3D"MARGIN: 0in 0in 0pt"><FONT color=3D#008000>University of=20
Phoenix</FONT></P>
<P style=3D"MARGIN: 0in 0in 0pt"><A=20
href=3D"mailto:kahager@email.uophx.edu">kahager@email.uophx.edu</A> </P>
<P style=3D"MARGIN: 0in 0in 0pt"><U><FONT color=3D#0000ff><A=20
href=3D"mailto:khagerman@email.wintu.edu">khagerman@email.wintu.edu</A></=
FONT></U></P>
<P style=3D"MARGIN: 0in 0in 0pt">206-309-0438 (Leave a=20
Message)</P></DIV></DIV></SPAN></STRONG></DIV></DIV></DIV>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Jay Freedman" &lt;<A=20
=
href=3D"mailto:jay.freedman@verizon.net">jay.freedman@verizon.net</A>&gt;=
wrote=20
in message <A=20
=
href=3D"news:%23Y4vTFq1GHA.2176@TK2MSFTNGP04.phx.gbl">news:%23Y4vTFq1GHA.=
2176@TK2MSFTNGP04.phx.gbl</A>...</DIV>Just=20
be careful of data types... the If statement should =
be<BR><BR>&nbsp;&nbsp;=20
if&nbsp; Val(ActiveDocument.FormFields("Qt0" &amp; i).Result) &lt;&gt; =
0=20
Then<BR><BR>because Val returns a number, not a string.<BR><BR>--=20
<BR>Regards,<BR>Jay Freedman<BR>Microsoft Word=20
MVP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FAQ: <A=20
href=3D"http://word.mvps.org">http://word.mvps.org</A><BR>Email cannot =
be=20
acknowledged; please post all follow-ups to the newsgroup so <BR>all =
may=20
benefit.<BR><BR>Karen wrote:<BR>&gt; Hiya =
TomorrowsMan,<BR>&gt;<BR>&gt; You=20
could check each field first to see if it is empty or using<BR>&gt; =
another=20
variable (j), test for an empty field, increment 'j' and use<BR>&gt; =
it as=20
your divisor.&nbsp; With your current code you could try =
this:<BR>&gt;<BR>&gt;=20
Sub QTotal()<BR>&gt;<BR>&gt; Dim aa As Single, i As Single, j As=20
Single<BR>&gt;<BR>&gt; aa =3D 0<BR>&gt; j =3D 0<BR>&gt; For i =3D 1 To =

5<BR>&gt;&nbsp;&nbsp;&nbsp; if&nbsp; =
Val(ActiveDocument.FormFields("Qt0" &amp;=20
i).Result) &lt;&gt; "" =
Then<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
aa =3D aa + Val(ActiveDocument.FormFields("Qt0" &amp;=20
i).Result)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j =3D j + =

1<BR>&gt;&nbsp;&nbsp;&nbsp; end if<BR>&gt; Next i<BR>&gt; aa =3D aa /=20
j<BR>&gt;<BR>&gt; ActiveDocument.FormFields("QTotal").Result =3D =
_<BR>&gt;=20
Format(aa, "#.##")<BR>&gt;<BR>&gt; End Sub<BR>&gt;<BR>&gt;<BR>&gt;=20
Karen<BR>&gt;<BR>&gt; "TomorrowsMan" &lt;<A=20
href=3D"mailto:tomorrowsman@gmail.com">tomorrowsman@gmail.com</A>&gt; =
wrote in=20
message<BR>&gt; <A=20
=
href=3D"news:1158088263.759420.45110@i42g2000cwa.googlegroups.com">news:1=
158088263.759420.45110@i42g2000cwa.googlegroups.com</A>...<BR>&gt;=20
- I have a table in Word 2000, 6 rows, 2 columns.<BR>&gt; - In the =
first=20
column, rows 1-5 are for review criteria, and in the<BR>&gt; second =
column is=20
a dropdown box in each cell with rating values from<BR>&gt; =
0-4.<BR>&gt; - The=20
user selects a rating from the dropdown box, then in the last<BR>&gt; =
cell of=20
the table (row 5, col 2), a formula calculate the =
average.<BR>&gt;<BR>&gt; The=20
problem is, not all of the fields are mandatory, so the number =
of<BR>&gt;=20
criteria can range from 1 to 5.&nbsp; Also, it is possible to get a=20
"0.0"<BR>&gt; rating.<BR>&gt;<BR>&gt; I had been using this; =
obviously, my=20
question involves how to make the<BR>&gt; value of i dynamic based on =
the=20
number of criteria used:<BR>&gt;<BR>&gt; Sub QTotal()<BR>&gt;<BR>&gt; =
Dim aa=20
As Single, i As Single<BR>&gt;<BR>&gt; aa =3D 0<BR>&gt; For i =3D 1 To =
5<BR>&gt;=20
aa =3D aa + Val(ActiveDocument.FormFields("Qt0" &amp; =
i).Result)<BR>&gt; Next=20
i<BR>&gt; aa =3D aa / 5<BR>&gt;<BR>&gt;=20
ActiveDocument.FormFields("QTotal").Result =3D _<BR>&gt; Format(aa,=20
"#.##")<BR>&gt;<BR>&gt; End Sub<BR>&gt;<BR>&gt; Thank =
you,<BR>&gt;<BR>&gt;=20
Chris <BR><BR></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_03D9_01C6D671.2D2F7050--


Re: Calculating in VBA with changing criteria by Karen

Karen
Tue Sep 12 15:23:54 CDT 2006

Hi TomorrowsMan,

Have to modify that suggested code given Jay's comment and your comment that
a field can be 0.00. We still have to check for no entry so.....

Sub QTotal()

Dim aa As Single, i As Single, j As Single

aa = 0
j = 0
For i = 1 To 5
if ActiveDocument.FormFields("Qt0" & i).Result <> "" Then
aa = aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
j = j + 1
end if
Next i
aa = aa / j

ActiveDocument.FormFields("QTotal").Result = _
Format(aa, "#.##")

End Sub



Karen Hagerman
Faculty
University of Phoenix
kahager@email.uophx.edu
khagerman@email.wintu.edu
206-309-0438 (Leave a Message)
"Karen" <wonderlover@functiy.com> wrote in message
news:OjSjGCq1GHA.1304@TK2MSFTNGP05.phx.gbl...
Hiya TomorrowsMan,

You could check each field first to see if it is empty or using another
variable (j), test for an empty field, increment 'j' and use it as your
divisor. With your current code you could try this:

Sub QTotal()

Dim aa As Single, i As Single, j As Single

aa = 0
j = 0
For i = 1 To 5
if Val(ActiveDocument.FormFields("Qt0" & i).Result) <> "" Then
aa = aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
j = j + 1
end if
Next i
aa = aa / j

ActiveDocument.FormFields("QTotal").Result = _
Format(aa, "#.##")

End Sub


Karen

"TomorrowsMan" <tomorrowsman@gmail.com> wrote in message
news:1158088263.759420.45110@i42g2000cwa.googlegroups.com...
- I have a table in Word 2000, 6 rows, 2 columns.
- In the first column, rows 1-5 are for review criteria, and in the
second column is a dropdown box in each cell with rating values from
0-4.
- The user selects a rating from the dropdown box, then in the last
cell of the table (row 5, col 2), a formula calculate the average.

The problem is, not all of the fields are mandatory, so the number of
criteria can range from 1 to 5. Also, it is possible to get a "0.0"
rating.

I had been using this; obviously, my question involves how to make the
value of i dynamic based on the number of criteria used:

Sub QTotal()

Dim aa As Single, i As Single

aa = 0
For i = 1 To 5
aa = aa + Val(ActiveDocument.FormFields("Qt0" & i).Result)
Next i
aa = aa / 5

ActiveDocument.FormFields("QTotal").Result = _
Format(aa, "#.##")

End Sub

Thank you,

Chris



Re: Calculating in VBA with changing criteria by TomorrowsMan

TomorrowsMan
Thu Sep 21 09:46:07 CDT 2006

Thanks so much!