Hey all. So, currently I created a leader-board function for a sport's league to determine which team is in 1st place, 2nd place, ect. The drawbacks is that it works for now and doesn't allow any for any new teams without writing more code and it is a long code (this version is a more condensed from the previous one). Can anyone, at the very least, help me to make it more condensed and/or make it to were new teams can be added without writing more code?
The result with the current code looks like this combined with the two tables and the variant is the rank number. The MatchTable is a table that has all the match information and the FactionTable is a range with all the Team Names:
You can change anything about the code as long as the result is the same the above table illustrates and it includes any new teams that might be created in the future. Thanks in advance
The result with the current code looks like this combined with the two tables and the variant is the rank number. The MatchTable is a table that has all the match information and the FactionTable is a range with all the Team Names:
Rank No. | Team Name |
1 | Team 1 |
2 | Team 2 |
3 | Team 3 |
4 | Team 4 |
5 | Team 5 |
6 | Team 6 |
7 | Team 7 |
8 | Team 8 |
9 | Team 9 |
You can change anything about the code as long as the result is the same the above table illustrates and it includes any new teams that might be created in the future. Thanks in advance
VBA Code:
Function LeaderBoard(MatchTable As Range, FactionTable As Range, Rank As Variant)
Val1 = 0
Val2 = 0
Val3 = 0
Val4 = 0
Val5 = 0
Val6 = 0
Val7 = 0
Val8 = 0
Val9 = 0
Var1 = 0
Var2 = 0
Var3 = 0
Var4 = 0
Var5 = 0
Var6 = 0
Fin = "The Finstock Exchange"
SW = "SWAG"
Den = "The Den"
Droog = "The Burning Droogs"
RS = "RoxStars"
US = "The Usual Suspects"
Dun = "The Dungeon"
QM = "The Quirky Mercs"
Kor = "Korruption"
For i = 1 To MatchTable.Rows.Count
If MatchTable.Cells(i, 4) = Fin Then
Out1 = Out1 + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win1 = Win1 + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def1 = Def1 + 1
End If
End If
If MatchTable.Cells(i, 4) = SW Then
Out2 = Out2 + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win2 = Win2 + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def2 = Def2 + 1
End If
End If
If MatchTable.Cells(i, 4) = Den Then
Out3 = Out3 + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win3 = Win3 + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def3 = Def3 + 1
End If
End If
If MatchTable.Cells(i, 4) = Droog Then
Out4 = Out4 + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win4 = Win4 + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def4 = Def4 + 1
End If
End If
If MatchTable.Cells(i, 4) = RS Then
Out5 = Out5 + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win5 = Win5 + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def5 = Def5 + 1
End If
End If
If MatchTable.Cells(i, 4) = US Then
Out6 = Out6 + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win6 = Win6 + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def6 = Def6 + 1
End If
End If
If MatchTable.Cells(i, 4) = Dun Then
Out7 = Out7 + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win7 = Win7 + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def7 = Def7 + 1
End If
End If
If MatchTable.Cells(i, 4) = QM Then
Out8 = Out8 + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win8 = Win8 + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def8 = Def8 + 1
End If
End If
If MatchTable.Cells(i, 4) = Kor Then
Out9 = Out9 + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win9 = Win9 + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def9 = Def9 + 1
End If
End If
Next i
'Fin
Val1 = Out1 + (Win1 / 100) + ((100 - Def1) / 10000)
'SWAG
Val2 = Out2 + (Win2 / 100) + ((100 - Def2) / 10000)
'Den
Val3 = Out3 + (Win3 / 100) + ((100 - Def3) / 10000)
'Droogs
Val4 = Out4 + (Win4 / 100) + ((100 - Def4) / 10000)
'RoxStars
Val5 = Out5 + (Win5 / 100) + ((100 - Def5) / 10000)
'Suspects
Val6 = Out6 + (Win6 / 100) + ((100 - Def6) / 10000)
'Dungeon
Val7 = Out7 + (Win7 / 100) + ((100 - Def7) / 10000)
'Mercs
Val8 = Out8 + (Win8 / 100) + ((100 - Def8) / 10000)
'Korrupt
Val9 = Out9 + (Win9 / 100) + ((100 - Def9) / 10000)
High = WorksheetFunction.Max(Val1, Val2, Val3, Val4, Val5, Val6, Val7, Val8, Val9)
Low = WorksheetFunction.Min(Val1, Val2, Val3, Val4, Val5, Val6, Val7, Val8, Val9)
Middle = WorksheetFunction.Median(Val1, Val2, Val3, Val4, Val5, Val6, Val7, Val8, Val9)
For s = 1 To FactionTable.Rows.Count
Value = 0
Out = 0
Win = 0
Def = 0
For i = 1 To MatchTable.Rows.Count
If MatchTable.Cells(i, 4) = FactionTable.Cells(s, 1) Then
Out = Out + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win = Win + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def = Def + 1
End If
End If
Next i
Value = Out + (Win / 100) + ((100 - Def) / 10000)
If Value = High Then
If Rank = 1 Then
cnt = FactionTable.Cells(s, 1)
End If
End If
If Value = Low Then
If Rank = 9 Then
cnt = FactionTable.Cells(s, 1)
End If
End If
If Value = Middle Then
If Rank = 5 Then
cnt = FactionTable.Cells(s, 1)
End If
End If
If Value < High And Value > Middle Then
If Value > Var3 Then
If Var3 = Var1 Then
Var3 = Value
End If
End If
If Value <> Var3 Then
If Var1 = Var2 Then
Var1 = Value
Else
If Value <> Var1 And Var3 Then
Var2 = Value
End If
End If
End If
End If
If Value < Middle And Value > Low Then
If Value > Var6 Then
If Var6 = Var4 Then
Var6 = Value
End If
End If
If Value <> Var6 Then
If Var4 = Var5 Then
Var4 = Value
Else
If Value <> Var4 And Var6 Then
Var5 = Value
End If
End If
End If
End If
Next s
High2 = WorksheetFunction.Max(Var1, Var2, Var3)
Low2 = WorksheetFunction.Min(Var1, Var2, Var3)
Mid2 = WorksheetFunction.Median(Var1, Var2, Var3)
High3 = WorksheetFunction.Max(Var4, Var5, Var6)
Low3 = WorksheetFunction.Min(Var4, Var5, Var6)
Mid3 = WorksheetFunction.Median(Var4, Var5, Var6)
For s = 1 To FactionTable.Rows.Count
Value = 0
Out = 0
Win = 0
Def = 0
For i = 1 To MatchTable.Rows.Count
If MatchTable.Cells(i, 4) = FactionTable.Cells(s, 1) Then
Out = Out + MatchTable.Cells(i, 24)
If MatchTable.Cells(i, 21) = "Winner" Then
Win = Win + 1 + (MatchTable.Cells(i, 23) / 1000)
Else
Def = Def + 1
End If
End If
Next i