首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel VBA组图表系列颜色

问Excel VBA组图表系列颜色
EN

Stack Overflow用户
提问于 2015-04-07 15:22:43
回答 1查看 807关注 0票数 0

我有一个程序,将提示用户打开文件,并从文件数据生成折线图。该系列将在同一图表上绘制。将有两个命令按钮:一个供用户选择文件;另一个用于生成图表。每次单击图表的命令按钮时,都会根据新打开的文件数据添加系列。

代码语言:javascript
复制
With ThisWorkbook.cht

For a = 1 To lastRow

' Add each series
    Set chtSeries = .SeriesCollection.NewSeries

    With chtSeries

        .Values = rng
        .XValues = Worksheets(sheet).Range(Worksheets(sheet).Cells(a, 1), Worksheets(sheet).Cells(a, 10))


    End With

    Next a

    End With

但是,我需要对序列行进行分组,从而使同一文件中的行用相同的颜色表示。

EN

回答 1

Stack Overflow用户

发布于 2015-04-07 17:16:09

下面是一些对您有帮助的详细代码:

代码语言:javascript
复制
Sub Graph()

Dim Gr As Chart

        Set Gr = ActiveWorkbook.Charts.Add
            With Gr
            'Whole data source
            .SetSourceData Source:=Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 5)), PlotBy:=xlRows
            'Graph type
            .ChartType = xlXYScatterSmooth
            'Place
            .Location Where:=xlLocationAsNewSheet, Name:=NewSheetName
            'Title
            .HasTitle = True
            .ChartTitle.Characters.Text = "Chart Title"

            For a = 1 To 20
                'Data Series 1
                .SeriesCollection.NewSeries
                .SeriesCollection(a).Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5)) 'change with a
                .SeriesCollection(a).XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1)) 'not changing I think
                .SeriesCollection(a).AxisGroup = 1
                .SeriesCollection(a).Name = "MTTF"
                '.SeriesCollection(i).Format.Line.Weight = 1
                '.SeriesCollection(i).Format.Line.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer) ' for a row/line
                '.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer)
            Next a



            'Axis parameters
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Age"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Text = "Hours"
            .PlotArea.Interior.ColorIndex = 2
            .Axes(xlValue).MajorGridlines.Border.LineStyle = xlDot
            .ChartArea.Font.Size = 14
            .Deselect
            End With

            'Legend positioning
            With ActiveChart.Legend
                .Left = 350
                .Top = 75
            End With
            'Drawing area positiong
            With ActiveChart.PlotArea
                .Width = 550
                .Height = 350
            End With



'Clean memory
Set Gr = Nothing



End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29486041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档