需求:
原始數(shù)據(jù)是多張表,
每張表中的內(nèi)容為同一班級一次考試的各科成績,
多張表意味著多次考試。
通過宏命令在菜單中建立一個命令按鈕,
能夠生成一個學(xué)生多次考試的單科/平均分等成績曲線圖。
Demo:
沒有網(wǎng)絡(luò)可以查資料,所以搞了好幾天晚上
今天終于弄完(還有好多情況沒有考慮)
通過生成一個匯總頁面方式做的圖
(正常應(yīng)該是引用多sheet頁的單元格,不清楚是不是這樣)
結(jié)果:
基本功能:根據(jù)選定單元格所在行,生成成績曲線圖
宏代碼,版本Office 2007
Sub 成績曲線圖()
'
' 成績曲線圖 Macro
'
'studentCode存放學(xué)號
Dim studentCode As String
studentCode = Selection.Value
'MsgBox (studentCode)
'單元格所在行
Dim cellRow, cellColumn As Integer
cellRow = ActiveCell.Row '活動單元格所在的行數(shù)
cellColumn = ActiveCell.Column '活動單元格所在的列數(shù)
'MsgBox (cellRow)
'MsgBox (cellColumn)
'刪除存在的個人匯總頁
Dim sheetsCount As Integer
For sheetsCount = 1 To Sheets.Count
If Sheets(sheetsCount).Name = "個人成績匯總" Then
'取消顯示提示框
Application.DisplayAlerts = False
Sheets("個人成績匯總").Select
ActiveWindow.SelectedSheets.Delete
'還原顯示提示框
Application.DisplayAlerts = True
Exit For
Else
End If
Next sheetsCount
'新建個人成績匯總頁
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "個人成績匯總"
'構(gòu)建行頭
Sheets(1).Select
Rows("1:1").Select
Selection.Copy
Sheets("個人成績匯總").Select
Rows("1:1").Select
ActiveSheet.Paste
'復(fù)制成績
Dim term As Integer
For term = 1 To Sheets.Count - 1
Sheets(term).Select
Rows(cellRow).Select
Selection.Copy
Sheets("個人成績匯總").Select
Rows(term + 1).Select
ActiveSheet.Paste
Next term
'處理列頭
For sheetsCount = 1 To Sheets.Count - 1
Cells(sheetsCount + 1, "A").Value = Sheets(sheetsCount).Name
Next sheetsCount
'設(shè)置A1單元格為學(xué)生名,并刪除姓名列
Range("A1").Value = Range("B2").Value
Columns(2).Delete
'計算每行各科考試平均分
Dim rowCount, colCount As Integer
rowCount = ActiveSheet.Range("A65535").End(xlUp).Row
colCount = ActiveSheet.Range("IV1").End(xlToLeft).Column
'MsgBox (rowCount)
'MsgBox (colCount)
'寫平均分四個字
Cells(1, colCount + 1).Value = "平均分"
'逐行計算平均分
Dim i, j As Integer
Dim sum As Integer
For i = 2 To rowCount
For j = 2 To colCount
sum = sum + Cells(i, j).Value
Next j
Cells(i, colCount + 1).Value = sum / (colCount - 1)
sum = 0
Next i
'選擇區(qū)域
'Range(Cells(1, 1), Cells(rowCount, colCount + 1)).Select
'制圖
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(Cells(1, 1), Cells(rowCount, colCount + 1))
ActiveChart.ChartType = xlLineMarkers
ActiveChart.PlotBy = xlColumns
'
End Sub