MarineMike1985
Programmer
Greeting all,
Thank you all very much for taking the time to look at this question.
BACKGROUND:
I am new to VBA but, not to programming. I am working on a way of tracking and evaluating fitness reports. The fitness reports have 14 different points of evaluations. Each point is given a score from A to H, with A being the worst, G the best, and H being a "Not observed or leave out.
ISSUE:
My issue with with a function I wrote which takes an array of the scores and finds the numerical average for the report (SEE CODE). A=1, B=2, etc. When this function is run at runtime and I try to return a value to a variable within another funtion I get a Type Mismatch error. But, when I wrote a "test" function to try an debug my error it works fine.
QUESTION:
Can anyone point out to me where I have screwed this up? Why does this work when I try and debug it but not at runtime?
Thank you very much for your time!
CODE:
Function ReportAverage(ByRef scores() As String)
Dim scr As Variant
'for Debugging print all of the scores given in the parameter
For Each scr In scores
Debug.Print scr
Next
Dim total As Double
Dim divider As Double
Dim score As Variant
Dim i As Integer
divider = UBound(scores)
For i = 1 To UBound(scores)
'If the score is "H" then that catagory is not counted in the average for this report
'Therefore the divider is reduced by 1
If (scores(i) = "H") Then
divider = divider - 1
Debug.Print "Skipped scores(" & i & ")"
GoTo Continue1
End If
Debug.Print "Scores(" & i & ") = " & scores(i)
'add the score to the total score
'LetterToScore is an additional function which converts the letter of a score into an integer
'which is then used to compute the average
total = total + LetterToScore(scores(i))
Continue1:
Next i
'The average of the report is simply the total score over the number of scores recorded
ReportAverage = total / divider
Debug.Print total / divider
End Function
Function testing()
Dim scores(1 To 14) As String
scores(1) = "D"
scores(2) = "D"
scores(3) = "D"
scores(4) = "D"
scores(5) = "D"
scores(6) = "D"
scores(7) = "D"
scores(8) = "D"
scores(9) = "D"
scores(10) = "D"
scores(11) = "D"
scores(12) = "D"
scores(13) = "D"
scores(14) = "D"
Debug.Print ReportAverage(scores)
End Function
Function SubmitBtn()
...
Dim scores(1 to 14) as String
Dim RptAvg as Double
...
'get the scores from the ReportForm
...
RptAvg = ReportAverage(scores) <- This is where I get the error
...
End Function
Thank you all very much for taking the time to look at this question.
BACKGROUND:
I am new to VBA but, not to programming. I am working on a way of tracking and evaluating fitness reports. The fitness reports have 14 different points of evaluations. Each point is given a score from A to H, with A being the worst, G the best, and H being a "Not observed or leave out.
ISSUE:
My issue with with a function I wrote which takes an array of the scores and finds the numerical average for the report (SEE CODE). A=1, B=2, etc. When this function is run at runtime and I try to return a value to a variable within another funtion I get a Type Mismatch error. But, when I wrote a "test" function to try an debug my error it works fine.
QUESTION:
Can anyone point out to me where I have screwed this up? Why does this work when I try and debug it but not at runtime?
Thank you very much for your time!
CODE:
Function ReportAverage(ByRef scores() As String)
Dim scr As Variant
'for Debugging print all of the scores given in the parameter
For Each scr In scores
Debug.Print scr
Next
Dim total As Double
Dim divider As Double
Dim score As Variant
Dim i As Integer
divider = UBound(scores)
For i = 1 To UBound(scores)
'If the score is "H" then that catagory is not counted in the average for this report
'Therefore the divider is reduced by 1
If (scores(i) = "H") Then
divider = divider - 1
Debug.Print "Skipped scores(" & i & ")"
GoTo Continue1
End If
Debug.Print "Scores(" & i & ") = " & scores(i)
'add the score to the total score
'LetterToScore is an additional function which converts the letter of a score into an integer
'which is then used to compute the average
total = total + LetterToScore(scores(i))
Continue1:
Next i
'The average of the report is simply the total score over the number of scores recorded
ReportAverage = total / divider
Debug.Print total / divider
End Function
Function testing()
Dim scores(1 To 14) As String
scores(1) = "D"
scores(2) = "D"
scores(3) = "D"
scores(4) = "D"
scores(5) = "D"
scores(6) = "D"
scores(7) = "D"
scores(8) = "D"
scores(9) = "D"
scores(10) = "D"
scores(11) = "D"
scores(12) = "D"
scores(13) = "D"
scores(14) = "D"
Debug.Print ReportAverage(scores)
End Function
Function SubmitBtn()
...
Dim scores(1 to 14) as String
Dim RptAvg as Double
...
'get the scores from the ReportForm
...
RptAvg = ReportAverage(scores) <- This is where I get the error
...
End Function