|
楼主 |
发表于 2024-12-21 16:50
|
显示全部楼层
本帖最后由 wangzqxa 于 2024-12-21 16:58 编辑
AI写的代码,无法达到效果
Sub CalculateScores()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("汇总表")
Dim i As Integer
Dim lastRow As Integer
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
For i = 3 To lastRow
' Calculate total physical score
ws.Cells(i, "S").Value = SumTop3(ws, i, Array("J", "L", "N", "P", "R"), Array("I", "K", "M", "O", "Q"))
' Calculate total skill score
ws.Cells(i, "AF").Value = SumTop3(ws, i, Array("U", "W", "Y", "AA", "AC", "AE"), Array("T", "V", "X", "Z", "AB", "AD"))
' Rank and determine grade
Dim totalScore As Double
totalScore = ws.Cells(i, "AJ").Value
Dim grade As String
grade = DetermineGrade(totalScore, ws.Cells(i, "AG").Value, ws.Cells(i, "AH").Value, ws.Cells(i, "S").Value, ws.Cells(i, "AF").Value)
ws.Cells(i, "AK").Value = grade
Next i
End Sub
Function SumTop3(ws As Worksheet, row As Integer, scoreCols As Variant, checkCols As Variant) As Double
Dim scores() As Double
Dim i As Integer, j As Integer, k As Integer
Dim temp As Double
' Initialize scores array
ReDim scores(0 To 0)
' Collect scores
For i = LBound(scoreCols) To UBound(scoreCols)
If ws.Cells(row, checkCols(i)).Value <> "" Then
If UBound(scores) = 0 And scores(0) = 0 Then
scores(0) = ws.Cells(row, scoreCols(i)).Value
Else
ReDim Preserve scores(UBound(scores) + 1)
scores(UBound(scores)) = ws.Cells(row, scoreCols(i)).Value
End If
End If
Next i
' Sort scores in descending order
For j = 0 To UBound(scores) - 1
For k = j + 1 To UBound(scores)
If scores(j) < scores(k) Then
temp = scores(j)
scores(j) = scores(k)
scores(k) = temp
End If
Next k
Next j
' Sum top 3 scores
Dim sum As Double
sum = 0
For i = 0 To Application.Min(2, UBound(scores))
sum = sum + scores(i)
Next i
SumTop3 = sum
End Function
Function DetermineGrade(totalScore As Double, controlScore As Double, theoryScore As Double, physicalTotal As Double, skillTotal As Double) As String
If totalScore >= 95 And totalScore <= 150 And theoryScore >= 91 And (controlScore >= 91 Or IsEmpty(controlScore)) Then
DetermineGrade = "一级"
ElseIf totalScore >= 91 And totalScore < 95 And theoryScore >= 81 And (controlScore >= 81 Or IsEmpty(controlScore)) Then
DetermineGrade = "二级"
ElseIf totalScore >= 81 And totalScore < 91 Then
DetermineGrade = "三级"
ElseIf totalScore >= 71 And totalScore < 81 Then
DetermineGrade = "四级"
ElseIf totalScore >= 60 And totalScore < 71 Then
DetermineGrade = "五级"
Else
DetermineGrade = "未达评级"
End If
End Function
|
|