在Excel中使用VBA將一列數(shù)字按照給定的比例自動分為等級,,可以通過以下示例代碼實(shí)現(xiàn): Sub AutoGrader()
Dim rng As Range
Dim cell As Range
Dim grades As Variant
Dim i As Integer
' 設(shè)置需要分級的數(shù)據(jù)范圍,,例如A列
Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:A10") ' 假設(shè)分析A1:A10
' 設(shè)置等級比例,,例如30%,,20%,50%
grades = Array(0.3, 0.2, 0.5)
' 遍歷范圍內(nèi)的每個單元格
For Each cell In rng
Dim sumGrades As Double
sumGrades = WorksheetFunction.Sum(grades)
' 根據(jù)比例分配等級
Dim rank As Integer
For rank = LBound(grades) To UBound(grades)
If cell.Value / sumGrades < grades(rank) Then
Exit For
End If
Next rank
' 根據(jù)等級賦予不同的值
Select Case rank
Case 0
cell.Offset(0, 1).Value = "優(yōu)"
Case 1
cell.Offset(0, 1).Value = "良"
Case 2
cell.Offset(0, 1).Value = "差"
End Select
Next cell
End Sub
|