1. 对商品库存的需求分析
代码段1-计算需求量并影藏无关行
Public Sub DemandCalc()
Application.ScreenUpdating = False
'定义用于循环的整型变量
Dim i As Integer
For i = 2 To 72
'判断库存量是否小于订购量
If Cells(i, 4) < Cells(i, 5) Then
'如果小于,则计算“需求量”
Cells(i, 6) = Cells(i, 5) - Cells(i, 4)
'选择"需求量"单元格
Cells(i, 6).Select
'为库存不足的"需求量"单元格设置底色
Selection.Interior.ColorIndex = 15
Else
'选择第i行
Rows(i).Select
'把库存剩余的行“隐藏”
Selection.EntireRow.Hidden = True
End If
Next i
Application.ScreenUpdating = True
End Sub
2. 对月销售情况进行分析
代码段2-汇总计算及图表绘制
Public Sub MonthlyCalc()
Application.ScreenUpdating = False
'定义用于循环的整型变量
Dim Itemp As Integer
Dim ChartTypeArray() As Variant
Dim ChartCount As Integer
For Itemp = 1 To 12
'计算商品(甲)的销售额合计
Cells(4, Itemp + 2) = Cells(2, Itemp + 2) * Cells(3, Itemp + 2)
'计算商品(乙)的销售额合计
Cells(7, Itemp + 2) = Cells(5, Itemp + 2) * Cells(6, Itemp + 2)
'计算商品(丙)的销售额合计
Cells(10, Itemp + 2) = Cells(8, Itemp + 2) * Cells(9, Itemp + 2)
'计算三种商品总的销售额合计
Cells(11, Itemp + 2) = Cells(4, Itemp + 2) + Cells(7, Itemp + 2) _
+ Cells(10, Itemp + 2)
Next Itemp
ChartTypeArray = Array(xlLineMarkers, xlLineMarkersStacked, xlLineMarkersStacked100)
ChartCount = 1
Do While (ChartCount <= (UBound(ChartTypeArray, 1) + 1))
'添加图表
Charts.Add
'定义图表类型
ActiveChart.ChartType = ChartTypeArray(ChartCount - 1)
'图表数据源
ActiveChart.SetSourceData Source:=Sheets("chap5_2").Range( _
"A1:N1,A4:N4,A7:N7,A10:N10"), PlotBy:=xlRows
'设置图表添加的位置
ActiveChart.Location Where:=xlLocationAsObject, Name:="chap5_2"
With ActiveChart
'使图表带有“标题”
.HasTitle = True
'设置图表“标题”
.ChartTitle.Characters.Text = "月销售情况对比"
'使图表带有X坐标标题
.Axes(xlCategory, xlPrimary).HasTitle = True
'设置图表X坐标标题
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "月份"
'使图表带有Y坐标标题
.Axes(xlValue, xlPrimary).HasTitle = True
'使图表带有Y坐标标题
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "合计(元)"
End With
With ActiveChart.Parent
.Left = 10 + 368 * (ChartCount - 1) '此处用以修改图表在工作表中的位置
.Top = 200
End With
ChartCount = ChartCount + 1
Loop
Application.ScreenUpdating = True
End Sub
效果图:
后记:
这个例子耗费了我两三个小时,不过也值了。学会了如何自动插入多张Excel图表。