|
本帖最后由 sunya_0529 于 2014-3-23 19:20 编辑
额外写了一个随机生成数据源的代码,再加上“一键制图”的代码,如下——
- Option Explicit
- Private Sub CommandButton1_Click() '随机生成源数据
- Application.ScreenUpdating = False
- Dim i%, j%, str$, s$, arr() As String
- [B4:K8].ClearContents '清空数据区域
- i = Int(Rnd() * 6 + 4) '随机取出若干品种
- ReDim arr(i) '重定义数组维度
- str = "123456789" '定义随机抽取品种母序列
- Do '循环抽取随机品种
- s = Val(Mid(str, Int(Rnd() * Len(str) + 1), 1))
- arr(j) = Range("A" & s + 12)
- str = Replace(str, s, "")
- j = j + 1
- Loop While j <= i
- arr(i) = "损益"
- [B4].Resize(1, i + 1) = arr '写入表头
- [B5].Resize(4, i) = "=INT(RAND()*89+10)" '生成10~99之间的随机数
- Range(ActiveSheet.Cells(5, i + 2), ActiveSheet.Cells(8, i + 2)) = "=INT(RAND()*98+1)-50" '生成±49~0之间的随机数
- [B5].Resize(4, i + 1).Copy
- [B5].Resize(4, i + 1).PasteSpecial xlPasteValues
- Application.CutCopyMode = False
- [B5].Select
- Application.ScreenUpdating = True
- End Sub
- Private Sub CommandButton2_Click() '一键制图
- '构建数据源区域
- With ActiveSheet
- Dim arr(15, 11), k As Range, i%, j%
- arr(0, 0) = "季度"
- arr(2, 0) = "Q1"
- arr(6, 0) = "Q2"
- arr(10, 0) = "Q3"
- arr(14, 0) = "Q4"
- j = [B4].End(xlToRight).Column - 1
- For Each k In Range([B4], .Cells(4, [B4].End(xlToRight).Column - 1))
- If Application.VLookup(k.Value, [A13:B21], 2, 0) = "水果" Then
- i = i + 1
- arr(0, i) = k
- arr(1, i) = k.Offset(1, 0)
- arr(5, i) = k.Offset(2, 0)
- arr(9, i) = k.Offset(3, 0)
- arr(13, i) = k.Offset(4, 0)
- Else
- j = j - 1
- arr(0, j) = k
- arr(2, j) = k.Offset(1, 0)
- arr(6, j) = k.Offset(2, 0)
- arr(10, j) = k.Offset(3, 0)
- arr(14, j) = k.Offset(4, 0)
- End If
- Next
- j = [B4].End(xlToRight).Column
- arr(0, j - 1) = .Cells(4, j)
- arr(3, j - 1) = .Cells(5, j)
- arr(7, j - 1) = .Cells(6, j)
- arr(11, j - 1) = .Cells(7, j)
- arr(15, j - 1) = .Cells(8, j)
- arr(0, j) = .Cells(4, j)
- arr(3, j) = -Val(.Cells(5, j).Value)
- arr(7, j) = -Val(.Cells(6, j).Value)
- arr(11, j) = -Val(.Cells(7, j).Value)
- arr(15, j) = -Val(.Cells(8, j).Value)
- Sheets(2).Cells.ClearContents
- Sheets(2).[A1].Resize(16, 12) = arr
- End With
- '生成图表
- Dim w As Shape, x As Chart, t%
- For Each w In ActiveSheet.Shapes
- If w.Type = msoChart Then w.Delete
- Next
- t = Sheets(2).[A1].End(xlToRight).Column - 1
- Set x = ActiveSheet.Shapes.AddChart.Chart
- With x
- .ChartType = xlColumnStacked
- .SetSourceData Source:=Sheets(2).[B1].Resize(16, t) '选择数据源
- .PlotBy = xlColumns
- .ChartGroups(1).GapWidth = 0 '列簇间距为0
- .Legend.Position = xlLegendPositionTop '图例项置顶显示
- .Legend.LegendEntries(t).Delete
- .Axes(xlCategory).MajorTickMark = xlNone '无横坐标刻度线
- .Axes(xlCategory).TickLabelPosition = xlLow '横坐标标签位置置底
- .SeriesCollection(1).XValues = "=Sheet2!$A$2:$A$16" '设置横坐标标签
- .SeriesCollection(t).ApplyDataLabels
- .SeriesCollection(t).DataLabels.Position = xlLabelPositionInsideBase
- .SeriesCollection(t).DataLabels.NumberFormatLocal = "[红色]-0;0;;"
- .SeriesCollection(t).Format.Fill.Visible = msoFalse
- .Axes(xlValue).MajorTickMark = xlNone '无主纵坐标轴刻度线
- .Axes(xlValue).TickLabelPosition = xlTickLabelPositionHigh '主纵坐标轴标签显示在右侧
- .Axes(xlValue).Format.Line.Visible = msoFalse '无主纵坐标轴线
- End With
- Set x = Nothing
- End Sub
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
评分
-
1
查看全部评分
-
|