Excel的強大優(yōu)勢還在于它提供的宏語言Visual Basic for Application(VBA).Visual Basic是windows環(huán)境下開發(fā)應用軟件的一種通用程序設計語言,功能強大,簡便易用。VBA是它的一個子集,可以廣泛地應用于Microsoft公司開發(fā)的各種軟件中,例如Word、Excel、Access等。
Sub 分班()
Const studentno = 191 '學生人數
Const zdno = 12 '字段數
Dim zd$(zdno) '定義為12個字段的數組
Dim a(studentno, zdno), stu(60, zdno)
Dim nam$(studentno), bjname$(60) '定義一個存放全校學生名字及各班學生名字的數組
Dim bj(studentno) '定義存放班級的一個數組
'理科班工作表
Sheets("高三理").Select
For i = 2 To studentno
bj(i) = ActiveSheet.Cells(i, 1)
nam$(i) = ActiveSheet.Cells(i, 2)
For j = 3 To zdno
a(i, j) = ActiveSheet.Cells(i, j)
Next j
Next i
'存放字段到數組中。
For i = 1 To zdno
zd$(i) = ActiveSheet.Cells(1, i)
Next i
'先建立各個班級的工作表
Sheets("高三理").Select
Sheets("高三理").Copy After:=Sheets("分數段")
Sheets("高三理 (2)").Select
Sheets("高三理 (2)").Name = "33"
For i = 2 To studentno
For j = 1 To zdno
ActiveSheet.Cells(i, j) = Space$(1)
Next j
Next i
Range("a1").Select
For i = 1 To zdno
ActiveSheet.Cells(1, i) = zd$(i)
Next i
'34到36班工作表的建立
For i = 34 To 36
x$ = Mid$(Str(33), 2)
Sheets(x$).Select
Sheets(x$).Copy After:=Sheets("分數段")
Sheets(x$ + " (2)").Select
Sheets(x$ + " (2)").Name = Mid$(Str(i), 2)
Next i
'具體分班。
For k = 33 To 36
bjrs = 0
x$ = Mid$(Str(k), 2)
no = k Mod 10
Sheets(x$).Select
For i = 2 To studentno
If bj(i) = no Then
bjrs = bjrs + 1
bjname$(bjrs) = nam$(i)
For j = 3 To zdno
stu(bjrs, j) = a(i, j)
Next j
End If
Next i
For i = 2 To bjrs
ActiveSheet.Cells(i, 1) = no
ActiveSheet.Cells(i, 2) = bjname$(i)
For j = 3 To zdno
ActiveSheet.Cells(i, j) = stu(i, j)
Next j
Next i
Next k
End Sub
二.總分
Const studentno = 190
Const xknum = 6
Const zdnum = 12
Sheets("高三理").Select
For i = 2 To studentno + 1
Sum = 0
For j = 1 To xknum
Sum = Sum + ActiveSheet.Cells(i, j + 3)
Next j
ActiveSheet.Cells(i, zdnum-1) = Sum
Next i
End Sub
三.平均分
Sub 平均分()
Const studentno = 190
Const xknum = 6
Dim fs(studentno, xknum), pjf3(4, 6), bjrs(4), qxpjf(6)
Dim bj(studentno)
Sheets("高三理").Select
'以下程序段用于求全校平均分
For i = 1 To studentno
bj(i) = ActiveSheet.Cells(i + 1, 1)
For j = 1 To xknum
fs(i, j) = ActiveSheet.Cells(i + 1, j + 3)
Next j
Next i
For i = 1 To xknum
Sum = 0
For j = 1 To studentno
um = Sum + fs(j, i)
Next j
qxpjf(i) = Sum / (j - 1)
Next i
'以下程序段用于求各班平均分
For j = 1 To 4
For i = 1 To studentno
Ifbj(i) = j + 2 Then
bjrs(j) = bjrs(j) + 1
For k = 1 To xknum
pjf3(j, k) = pjf3(j, k) + fs(i, k)
Next k
End If
Next i
Next j
For j = 1 To 4
For i = 1 To 6
pjf3(j, i) = pjf3(j, i) / bjrs(j)
Next i
Next j
'寫入各班各科平均分
Sheets("平均分").Select
For i = 1 To 4
For j = 1 To 6
ActiveSheet.Cells(i + 2, j + 1) = pjf3(i, j)
Next j
Next i
'寫入全校各科平均分
i = 7
For j = 1 To 6
ActiveSheet.Cells(i, j + 1) = qxpjf(j)
Next j
End Sub