Hi Lewis
Massive thanks for the sample workbook.
Having studied your codes, I've created attached workbook.
I thought I could simplify the codes by putting Base Name in an array and do nested For Loops.
This, however, does not work, stops at 28th line "Next k" saying that there's no For statement.
I don't understand why because I meant the line 20 ("For k = 1 to j") to be the one to pair it to form the loop.
I wouldn't be surprised if there were more bugs but could you have a look at it to see what I'm doing wrong?
Many many thanks.
Maki
Experiment.xls
Sub Calculate()
Dim ctrl As Control
Dim judgecategory As Variant
Dim i, j, k As Integer
Dim iscore, maxscore, subtotal As Double
Dim ctrlname, scoreboxname, 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 & "Score" Then
j = j + 1
End If
Next ctrl
subtotal = 0
For k = 1 To j
scoreboxname = CStr("txt" & judgecategory & "Score" & k)
maxscorelabelname = CStr("lbl" & judgecategory & "MaxP" & k)
iscore = CDbl(Me.Controls(scoreboxname).Value)
maxscore = CDbl(Me.Controls(maxscorelabelname).Caption)
If iscore > maxscore Then
Me.Controls(scoreboxname).BackColor = vbRed
subtotal = subtotal + iscore
Next k
Me.Controls("txt" & judgecategory & "Subtotal").Value = subtotal
Next i
End Sub
Bookmarks