Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用vba在Excel中编辑图表_Vba_Excel - Fatal编程技术网

使用vba在Excel中编辑图表

使用vba在Excel中编辑图表,vba,excel,Vba,Excel,我想调整x轴和y轴,以使用放置图表的同一工作表中特定范围的值。 而且,每一列都有不同的颜色(图表上有图例) 以下是我目前的代码: Dim cht As ChartObject Set chtChart = Worksheets("Sheet1").ChartObjects.Add(Left:=75, Width:=300, Top:=75, Height:=300).Chart With chtChart .ChartType = xlColumnStacked

我想调整x轴和y轴,以使用放置图表的同一工作表中特定范围的值。 而且,每一列都有不同的颜色(图表上有图例) 以下是我目前的代码:

   Dim cht As ChartObject
   Set chtChart = Worksheets("Sheet1").ChartObjects.Add(Left:=75, Width:=300, Top:=75, Height:=300).Chart
   With chtChart
    .ChartType = xlColumnStacked
   End With

编辑:我可以只使用图表,它不必是ChartObject。但当我只使用“图表”时,会创建另一个工作表(除了将图表放在所需的工作表上),我希望避免这种情况。

以下是一些代码,可以帮助您:

Sub Graph()

Dim Gr As Chart

        Set Gr = ActiveWorkbook.Worksheets("Sheet1").ChartObjects.Add(Left:=75, Width:=300, Top:=75, Height:=300).Chart
            With Gr
            'Définition des données sources du graphique
            .SetSourceData Source:=Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 5)), PlotBy:=xlRows
            'Type de graphique
            .ChartType = xlColumnStacked
            'Place
            .Location Where:=xlLocationAsNewSheet, Name:=NewSheetName
            'Titre
            .HasTitle = True
            .ChartTitle.Characters.Text = "Chart Title"
            'Data Series 1
            .SeriesCollection.NewSeries
            .SeriesCollection(1).Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5))
            .SeriesCollection(1).XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1))
            .SeriesCollection(1).AxisGroup = 1
            .SeriesCollection(1).Name = "MTTF"
            'Data Series 2
            .SeriesCollection.NewSeries
            .SeriesCollection(2).Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5))
            .SeriesCollection(2).XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1))
            .SeriesCollection(2).Name = "MTTR"
            'Second axis
            .SeriesCollection(2).AxisGroup = 2
            '.SeriesCollection(3).Delete
            '.SeriesCollection(i).Format.Line.Weight = 1
            '.SeriesCollection(i).Format.Line.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer) ' pour une ligne
            '.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer) ' pour une area

            '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

您只需调整您的数据选择和名称,这应该是一个良好的开端

,如果这足以让您验证答案以关闭主题