很抱歉,如果有人回答了这个问题……我看了看,似乎应该有可能,但是这里有。我正在尝试学习VBA,并且正在从事一项练习。任务是平均两列考试成绩(“ C4:C31”和“ D4:D31”)并返回字母成绩(在“ E4:E31”中),然后创建一个柱状图,显示这些成绩的分布。我已经完成了第一部分,并获得了令我满意的结果。我在图表部分遇到了麻烦。
我可以在工作簿中添加一个表,然后使用“ SetSourceData”链接到工作簿上的该范围。但是,我想尝试(如果可能的话)在VBA中添加数据。我想在X轴上分别绘制A / B / C / D / F与获得每个等级的次数的图表,并为每个等级产生一个条形图。我知道我的成绩只会是A / B / C / D / F,而我的成绩以下(ACount,BCount,CCount,DCount,FCount)。我正在尝试将定义的数据添加到我在VBA中创建的图表中。
很抱歉,如果这很简单-我正在尝试学习这种机制,这就是为什么我正在寻找一种更“困难”的方式来做到这一点!
Dim GradeOutput As Range
Dim GradeLocation As Range
Dim GradeNumber As Double
Dim GradeLetter As String
Set GradeOutput = Range(Range("E4"), Range("B4").End(xlDown).Offset(0, 3))
For Each GradeLocation In GradeOutput
GradeLocation.Value = Application.Average(GradeLocation.Offset(0, -2).Value, GradeLocation.Offset(0, -1).Value)
GradeNumber = GradeLocation.Value
Select Case GradeNumber
Case Is >= 90
GradeLetter = "A"
Case Is >= 80
GradeLetter = "B"
Case Is >= 70
GradeLetter = "C"
Case Is >= 60
GradeLetter = "D"
Case Else
GradeLetter = "F"
End Select
GradeLocation.Value = GradeLetter
Next
Dim ACount As Integer
Dim BCount As Integer
Dim CCount As Integer
Dim DCount As Integer
Dim FCount As Integer
Dim GradeChart As Chart
Dim ChartShape As Shape
Dim ChartLocation As Range
Dim GradeChartTitle As String
ACount = Application.WorksheetFunction.CountIf(GradeOutput, "A")
BCount = Application.WorksheetFunction.CountIf(GradeOutput, "B")
CCount = Application.WorksheetFunction.CountIf(GradeOutput, "C")
DCount = Application.WorksheetFunction.CountIf(GradeOutput, "D")
FCount = Application.WorksheetFunction.CountIf(GradeOutput, "F")
Set ChartLocation = Range("J3:O13")
Set ChartShape = ActiveSheet.Shapes.AddChart2(201, xlColumnClustered)
Set GradeChart = ChartShape.Chart
With ChartShape
.Left = ChartLocation.Left
.Width = ChartLocation.Width
.Top = ChartLocation.Top
.Height = ChartLocation.Height
.Name = "GradeChart"
End With
GradeChartTitle = "Distribution of Grades"
With GradeChart
.ChartType = xlColumnClustered
'.PlotBy = xlColumns
.HasTitle = True
.ChartTitle.Text = GradeChartTitle
.SeriesCollection(1).XValues = "A", "B", "C", "D", "F"
.SeriesCollection(1).Values = ACount, BCount, CCount, DCount, FCount
End With