从图表图例中删除系列并在excel vba中设置轴范围

时间:2017-02-08 14:37:35

标签: excel-vba vba excel

我正在使用VBA并且我正在尝试安装图表,并且我试图使y轴具有多年的值。然而,一个新的系列出现了,名为17系列,出现在传奇中。无论如何都要从图例中删除它,但不能将其作为x轴值删除。我已经尝试过" MyChartObj.Chart.SeriesCollection(17)。删除"但这会将其删除为y轴值。

也有人知道如何在轴上设置值的范围。例如,我希望图表介于0到100之间,而不是当前正在发生的0和120.

这是我的代码:

Sub orange_weightings_chart()

Dim MyChtObj As ChartObject
Dim Sht1 As Worksheet
Dim Sht2 As Worksheet
Dim Sht1Name As String
Dim Sht2Name As String

Set Sht2 = Worksheets("Orange Weightings")
Set Sht1 = Worksheets("Weightings Table")
Sht1Name = Sht1.Name
Sht2Name = Sht2.Name
Set MyChtObj = Sht1.ChartObjects.Add(100, 100, 500, 500)

Set a = Sht1.Range("A2", Sht1.Range("A2").End(xlToRight))
Set b = Sht1.Range("A3", Sht1.Range("A3").End(xlToRight))
Set c = Sht1.Range("A4", Sht1.Range("A4").End(xlToRight))
Set d = Sht1.Range("A5", Sht1.Range("A5").End(xlToRight))
Set e = Sht1.Range("A6", Sht1.Range("A6").End(xlToRight))
Set f = Sht1.Range("A7", Sht1.Range("A7").End(xlToRight))
Set g = Sht1.Range("A8", Sht1.Range("A8").End(xlToRight))
Set h = Sht1.Range("A9", Sht1.Range("A9").End(xlToRight))
Set i = Sht1.Range("A10", Sht1.Range("A10").End(xlToRight))
Set j = Sht1.Range("A11", Sht1.Range("A11").End(xlToRight))
Set k = Sht1.Range("A12", Sht1.Range("A12").End(xlToRight))
Set l = Sht1.Range("A13", Sht1.Range("A13").End(xlToRight))
Set m = Sht1.Range("A14", Sht1.Range("A14").End(xlToRight))
Set n = Sht1.Range("A15", Sht1.Range("A15").End(xlToRight))
Set o = Sht1.Range("A16", Sht1.Range("A16").End(xlToRight))
Set p = Sht1.Range("A17", Sht1.Range("A17").End(xlToRight))
Set InputData = Union(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)

With MyChtObj.Chart
    .ChartType = xlBarStacked
    .SetSourceData InputData
    .PlotBy = xlRows
    .HasTitle = True
    .ChartTitle.Text = "Original Forecast Parameter Weightings"
    .HasLegend = True
    .SeriesCollection.NewSeries.XValues = Sht1.Range("B1", Sht1.Range("B1").End(xlToRight))
End With

With MyChtObj
    .Top = Sht2.Range("E15").Top
    .Left = Sht2.Range("E15").Left
    .Width = 1200
    .Height = 380
End With

End Sub

提前致谢,

最高

1 个答案:

答案 0 :(得分:1)

要仅从图例中删除Series 17(而不是从图表中删除),并将Chart轴从0设置为100,请使用以下代码:

With MyChtObj.Chart
    .Legend.LegendEntries(17).Delete  ' delete the legend only

    .Axes(xlValue).MinimumScale = 0 ' set minimum value
    .Axes(xlValue).MaximumScale = 100  ' set maximum value
End With