Hi Maki and Norie,
Thanks Norie for updating Maki's code. In Norie's code I got a run time error when one of the Score Text Boxes was BLANK or contained a non-numeric character. See the attached (hopefully) corrected workbook and code that follows.
The dangling If without an Endif caused the for loop compile error.
Norie's changes in GREEN. My changes in Red:
Sub CalculateScores()
Dim ctrl As MSForms.Control
Dim judgecategory As Variant
Dim i As Long, j As Long, k As Long
Dim iscore As Double, maxscore As Double, subtotal As Double
Dim ctrlname As String, scoreboxname As String, maxscorelabelname As String
judgecategory = Array("RoutineContent", "TechnicalMerit", "MusicalInterpretation")
For i = LBound(judgecategory) To UBound(judgecategory)
j = 0
For Each ctrl In Me.Controls
ctrlname = ctrl.Name
If Left(ctrlname, Len(ctrlname) - 1) = "txt" & judgecategory(i) & "Score" Then
j = j + 1
End If
Next ctrl
subtotal = 0
For k = 1 To j
scoreboxname = CStr("txt" & judgecategory(i) & "Score" & k)
maxscorelabelname = CStr("lbl" & judgecategory(i) & "MaxP" & k)
iscore = 0
On Error Resume Next
iscore = CDbl(Me.Controls(scoreboxname).Value)
On Error GoTo 0
maxscore = CDbl(Me.Controls(maxscorelabelname).Caption)
If iscore > maxscore Then
Me.Controls(scoreboxname).BackColor = vbRed
End If
subtotal = subtotal + iscore
Next k
Me.Controls("txt" & judgecategory(i) & "Subtotal").Value = subtotal
Next i
End Sub
Bookmarks