爱问知识人 爱问教育 医院库

excel2007制作柏拉图问题

首页

excel2007制作柏拉图问题

刚刚在网上看到一个哥们用VBA在EXCEL2010下做了段程序,可自动生成柏拉图。
小弟愚昧,不清楚如何使用下面的代码来自动生成柏拉图,哪位大哥知道,请告诉兄弟,最好上传个excel样例!兄弟不胜感激!

以下是程序源代码

'数据区域的左上角位置常量
Const UpperLeftPos = "B4"
Const UpperPos = "B"
Const LeftPos = 4

Sub 绘制标准柏拉图()
    Dim Range1 As Range
        
    '选择数据区域,Range("B2")这个格子里放的是有效数据的行数,即分类的总个数
    Set Range1 = Range(UpperLeftPos & ":" & Chr(Asc(UpperPos) + 1) & (LeftPos + Range("B2")) & "," & Chr(Asc(UpperPos) + 2) & LeftPos & ":" & Chr(Asc(UpperPos) + 2) & (LeftPos + Range("B2") + 1))
    
    '插入柱形图
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Range1
    
    '把百分比数据系列的图形格式变为“带数据值的折线图”,并画在由次要横坐标和次要纵坐标里
    ActiveChart.SeriesCollection(2).Select
    ActiveChart.SeriesCollection(2).AxisGroup = 2
    ActiveChart.SeriesCollection(2).ChartType = xlLineMarkers
    ActiveChart.SetElement (msoElementSecondaryCategoryAxisWithoutLabels)
    ActiveChart.SetElement (msoElementSecondaryCategoryAxisShow)
    
    '把次要横坐标的值显示到刻度上,而不是刻度中间,并隐藏次要横坐标的显示
    ActiveChart.Axes(xlCategory, xlSecondary).Select
    Selection.MajorTickMark = xlNone
    Selection.TickLabelPosition = xlNone
    ActiveChart.Axes(xlCategory, xlSecondary).AxisBetweenCategories = False
    
    '调整柱形图的显示,去掉相邻柱子的间距,并给每个柱子加外框
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.ChartGroups(1).GapWidth = 0
    With Selection.Format.Line
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorText1
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 1
    End With
    
    '修改主要纵坐标,满值为累加和
    ActiveChart.Axes(xlValue).Select
    ActiveChart.Axes(xlValue).CrossesAt = 0
    Selection.MajorTickMark = xlInside
    ActiveChart.Axes(xlValue).MinimumScale = 0
    ActiveChart.Axes(xlValue).MaximumScale = Range(Chr(Asc(UpperPos) + 3) & (LeftPos + Range("B2") + 1))
    
    '修改次要纵坐标,满值为100%
    ActiveChart.Axes(xlValue, xlSecondary).Select
    ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = 1
    ActiveChart.Axes(xlValue, xlSecondary).MinimumScale = 0
    Selection.MajorTickMark = xlInside
    ActiveChart.Axes(xlValue, xlSecondary).CrossesAt = 1
    Selection.TickLabels.NumberFormat = "0%"
    
    '让百分比折线上的点显示出具体的数值
    ActiveChart.SeriesCollection(2).Select
    ActiveChart.SeriesCollection(2).ApplyDataLabels
    ActiveChart.SeriesCollection(2).DataLabels.Select
    Selection.Position = xlLabelPositionRight
    
    '隐去横向的主要网格线
    ActiveChart.Axes(xlValue).Select
    ActiveChart.Axes(xlValue).MajorGridlines.Select
    Selection.Format.Line.Visible = msoFalse
    
    '为图表添加标题,标题的内容放在Range("B1")格子里
    ActiveChart.SetElement (msoElementChartTitleAboveChart)
    ActiveChart.ChartTitle.Select
    ActiveChart.ChartTitle.Text = Range("B1")
End Sub

提交回答
好评回答
  • 2011-01-10 16:04:24
    您可以这样操作:
    把这些代码粘贴至Excel的VBA代码窗口中,然后保存,最后运行这个EXCEL文件,您就可以看到效果了。

    _***

    2011-01-10 16:04:24

类似问题

换一换
  • 软件 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):