Dzwebs.Net

撰写电脑技术杂文十余年

VBA操作Excel之中学通用成绩管理示例下篇

Admin | 2008-7-14 7:41:19 | 被阅次数 | 7395

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

        接上篇,以下为VBA代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'以下求各班级各学科平均分
startclass = 1 '在表一检查班级名称的行序号初始值
endclass = 40 '在表一检查班级名称的行序号终止值

Startscan = 2 '在表四扫描学生数据之行号初始值
endscan = 2000 '在表四扫描学生数据之行号终止值

StartSubject = 3 '在表一要查找的科目列序号初始值
EndSubject = 8 '在表一要查找的科目列序号之终止值

total = 0 '总分
stunum = 0 '学生人数

TotalSheetIndex = 1 '汇总表序号
StuScorseSheetIndex = 4 '学生成绩表序号

If Target.Column = 13 And Target.Row = 4 Then
For m = StartSubject To EndSubject '求每个班级每个科目的总平均分
  For k = startclass To endclass
    calssname = Worksheets(TotalSheetIndex).Cells(k, 2).Value
    If Len(calssname) <> 0 And calssname <> "班级" Then '检查班级号是否为空或其他非班级号字符
         For h = Startscan To endscan
        '检查班级相同
           If Worksheets(StuScorseSheetIndex).Cells(h, 3).Value = Worksheets(TotalSheetIndex).Cells(k, 2).Value Then
              total = total + Worksheets(StuScorseSheetIndex).Cells(h, 7 + (m - 3)).Value
             If Len(Worksheets(StuScorseSheetIndex).Cells(h, 5).Value) <> 0 Then '检查学生姓名不为空
              stunum = stunum + 1
           End If
          End If
        Next
    If total <> 0 And stunum <> 0 Then
      Worksheets(TotalSheetIndex).Cells(k, m).Value = total / stunum
    Else
      Worksheets(TotalSheetIndex).Cells(k, m).Value = 0
    End If
    total = 0
    stunum = 0
    End If
  Next
Next

'以下求各班级总平均分
For k = startclass To endclass
    calssname = Worksheets(TotalSheetIndex).Cells(k, 2).Value
    If Len(calssname) <> 0 And calssname <> "班级" Then '检查班级号是否为空或其他非班级号字符
        For h = Startscan To endscan
        '检查班级相同
        If Worksheets(StuScorseSheetIndex).Cells(h, 3).Value = Worksheets(TotalSheetIndex).Cells(k, 2).Value Then
            For m = StartSubject To EndSubject
              total = total + Worksheets(StuScorseSheetIndex).Cells(h, 7 + (m - 3)).Value
            Next
           
            If Len(Worksheets(StuScorseSheetIndex).Cells(h, 5).Value) <> 0 Then '检查学生姓名不为空
            stunum = stunum + 1
           End If
        End If
      Next
      If total <> 0 And stunum <> 0 Then
        Worksheets(TotalSheetIndex).Cells(k, m).Value = total / stunum
      Else
        Worksheets(TotalSheetIndex).Cells(k, m).Value = 0
      End If
      total = 0
      stunum = 0
      End If
Next
End If
End Sub


该杂文来自: Excel杂文

上一篇:VBA操作Excel之中学通用成绩管理示例上篇

下一篇:一览Excel工作表、工作薄对象之事件表

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计