excel按照班级统计分数段的vba代码
时间: 2024-05-11 14:17:46 浏览: 120
以下是一个基本的VBA代码,可以按照班级统计分数段:
```
Sub ScoreAnalysis()
Dim i As Integer '循环计数器
Dim j As Integer '循环计数器
Dim k As Integer '循环计数器
Dim n As Integer '循环计数器
Dim ClassNum As Integer '班级数
Dim SegmentNum As Integer '分数段数
Dim ScoreRange As Integer '分数段宽度
Dim MaxScore As Integer '最高分
Dim MinScore As Integer '最低分
Dim TotalScore() As Integer '总分数组
Dim SegmentScore() As Integer '分数段数组
Dim ClassScore() As Integer '班级分数数组
Dim ClassSegment() As Integer '班级分数段数组
ClassNum = 10 '班级数
SegmentNum = 5 '分数段数
ScoreRange = 20 '分数段宽度
MaxScore = 100 '最高分
MinScore = 0 '最低分
ReDim TotalScore(1 To ClassNum) '重新定义数组大小
ReDim SegmentScore(1 To SegmentNum) '重新定义数组大小
ReDim ClassScore(1 To ClassNum) '重新定义数组大小
ReDim ClassSegment(1 To ClassNum, 1 To SegmentNum) '重新定义数组大小
'初始化数组
For i = 1 To ClassNum
TotalScore(i) = 0
ClassScore(i) = 0
For j = 1 To SegmentNum
ClassSegment(i, j) = 0
Next j
Next i
'输入分数
For i = 1 To ClassNum
For j = 1 To 30
n = Int(Rnd() * 100) '随机数生成分数
TotalScore(i) = TotalScore(i) + n
If n >= MaxScore Then '判断最高分
n = MaxScore
End If
If n <= MinScore Then '判断最低分
n = MinScore
End If
For k = 1 To SegmentNum '判断分数段
If n >= ((k - 1) * ScoreRange) And n <= (k * ScoreRange) Then
ClassSegment(i, k) = ClassSegment(i, k) + 1
End If
Next k
Next j
Next i
'计算平均分和各分数段的人数
For i = 1 To ClassNum
ClassScore(i) = TotalScore(i) / 30 '计算平均分
For j = 1 To SegmentNum
SegmentScore(j) = SegmentScore(j) + ClassSegment(i, j)
Next j
Next i
'输出结果
For i = 1 To SegmentNum
Range("A" & i + 1).Value = "分数段" & i '输出分数段
Range("B" & i + 1).Value = (i - 1) * ScoreRange & "-" & i * ScoreRange '输出分数段范围
Range("C" & i + 1).Value = SegmentScore(i) '输出各分数段的人数
Next i
For i = 1 To ClassNum
Range("D" & i + 1).Value = "班级" & i '输出班级
Range("E" & i + 1).Value = ClassScore(i) '输出平均分
For j = 1 To SegmentNum
Range("F" & i + 1 + j).Value = "分数段" & j '输出分数段
Range("G" & i + 1 + j).Value = ClassSegment(i, j) '输出各分数段的人数
Next j
Next i
End Sub
```
需要注意的是,这个代码中的分数是随机生成的,你可以根据实际情况修改代码。同时,输出的结果也是在Excel表格中,你可以根据自己的需要进行修改。
阅读全文