S
Steve Finlayson
I am creating an application that goes through 6 columns of data and
generates an average. I have got the code working for each single
column and am trying to create the best method of going to each
succesive column. first I am showing the code now used and then the
code that I think I should use. I am not sure that it is correct
though. Could someone look over the second set of code and make
suggestion or corrections. I am still learning. Thanks
Steve
Here is the code that I have started with for two columns:
Dim i As Integer, mysum As Long, Counter As Integer
Application.ScreenUpdating = False
On Error GoTo ReturnScreen
ActiveDocument.Unprotect
mysum1 = 0
counter1 = 0
mysum2 = 0
counter2 = 0
mysum3 = 0
counter3 = 0
mysum4 = 0
counter4 = 0
mysum5 = 0
counter5 = 0
mysum6 = 0
counter6 = 0
' Col1
For i = 155 To 191
mysum1 = mysum1 + Val(ActiveDocument.FormFields("dropdown" &
i).Result)
If Val(ActiveDocument.FormFields("dropdown" & i).Result) <> 0 Then
counter1 = counter1 + 1
End If
ActiveDocument.FormFields("Text374").Result = mysum1 / counter1
Next i
' Col2
For i = 192 To 228
mysum2 = mysum2 + Val(ActiveDocument.FormFields("dropdown" &
i).Result)
If Val(ActiveDocument.FormFields("dropdown" & i).Result) <> 0 Then
counter2 = counter2 + 1
End If
ActiveDocument.FormFields("Text380").Result = mysum2 / counter2
Next i
==========
I think that it would be better to use an array, if I inderstand what
they are and how they work correctly. I am proposing to use this code
to replace the previous:
Dim astrMysum(0 To 6) as Long
Dim astrCounter(0 To 6) as Integer
Dim intIndex as Integer
' Col1
For i = 155 To 191
Mysum(Index) = mysum(Index) + Val(ActiveDocument.FormFields("dropdown"
& i).Result)
If Val(ActiveDocument.FormFields("dropdown" & i).Result) <> 0 Then
Counter(Index) = counter(Index) + 1
End If
ActiveDocument.FormFields("Text374").Result = mysum(Index) /
counter(Index)
Next i
' Col2
Index = Index + 1
For i = 192 To 228
mysum(Index) = mysum(Index) + Val(ActiveDocument.FormFields("dropdown"
& i).Result)
If Val(ActiveDocument.FormFields("dropdown" & i).Result) <> 0 Then
counter(Index) = counter(Index) + 1
End If
ActiveDocument.FormFields("Text380").Result = mysum(Index) /
counter(Index)
Next i
generates an average. I have got the code working for each single
column and am trying to create the best method of going to each
succesive column. first I am showing the code now used and then the
code that I think I should use. I am not sure that it is correct
though. Could someone look over the second set of code and make
suggestion or corrections. I am still learning. Thanks
Steve
Here is the code that I have started with for two columns:
Dim i As Integer, mysum As Long, Counter As Integer
Application.ScreenUpdating = False
On Error GoTo ReturnScreen
ActiveDocument.Unprotect
mysum1 = 0
counter1 = 0
mysum2 = 0
counter2 = 0
mysum3 = 0
counter3 = 0
mysum4 = 0
counter4 = 0
mysum5 = 0
counter5 = 0
mysum6 = 0
counter6 = 0
' Col1
For i = 155 To 191
mysum1 = mysum1 + Val(ActiveDocument.FormFields("dropdown" &
i).Result)
If Val(ActiveDocument.FormFields("dropdown" & i).Result) <> 0 Then
counter1 = counter1 + 1
End If
ActiveDocument.FormFields("Text374").Result = mysum1 / counter1
Next i
' Col2
For i = 192 To 228
mysum2 = mysum2 + Val(ActiveDocument.FormFields("dropdown" &
i).Result)
If Val(ActiveDocument.FormFields("dropdown" & i).Result) <> 0 Then
counter2 = counter2 + 1
End If
ActiveDocument.FormFields("Text380").Result = mysum2 / counter2
Next i
==========
I think that it would be better to use an array, if I inderstand what
they are and how they work correctly. I am proposing to use this code
to replace the previous:
Dim astrMysum(0 To 6) as Long
Dim astrCounter(0 To 6) as Integer
Dim intIndex as Integer
' Col1
For i = 155 To 191
Mysum(Index) = mysum(Index) + Val(ActiveDocument.FormFields("dropdown"
& i).Result)
If Val(ActiveDocument.FormFields("dropdown" & i).Result) <> 0 Then
Counter(Index) = counter(Index) + 1
End If
ActiveDocument.FormFields("Text374").Result = mysum(Index) /
counter(Index)
Next i
' Col2
Index = Index + 1
For i = 192 To 228
mysum(Index) = mysum(Index) + Val(ActiveDocument.FormFields("dropdown"
& i).Result)
If Val(ActiveDocument.FormFields("dropdown" & i).Result) <> 0 Then
counter(Index) = counter(Index) + 1
End If
ActiveDocument.FormFields("Text380").Result = mysum(Index) /
counter(Index)
Next i