VBA中柱形图周围的黑色边框

时间:2017-02-10 16:14:20

标签: excel vba excel-vba

我尝试在Vba CODE的柱形图周围设置黑色边框。这就是我现在拥有的。我设置Border.ColorIndex的最后一行显然不起作用。目前该列看起来像这样。

enter image description here

我希望它看起来像这样。

enter image description here

这是我的代码。

ActiveSheet.Cells(10000, 10000).Select
ActiveSheet.Shapes.AddChart.Select
With ActiveChart ' clear SeriesCollection
Do Until .SeriesCollection.Count = 0
         .SeriesCollection(1).Delete
Loop
End With
ActiveChart.ChartType = xlColumnClustered
ActiveChart.PlotVisibleOnly = True
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Plottt"     ' rename chart sheets

' create SeriesCollection for each line
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Name = "Hallo"
    ActiveChart.SeriesCollection(1).XValues = breaks
    ActiveChart.SeriesCollection(1).Values = freq
    ActiveChart.ChartGroups(1).GapWidth = 0
    ActiveChart.ChartGroups(1).Border.ColorIndex = 3

另外,我想从我的代码中减小步长。对此的帮助也将不胜感激。

2 个答案:

答案 0 :(得分:2)

要设置SeriesCollection(1)的边框颜色,请使用以下行:

ActiveChart.SeriesCollection(1).Format.Line.ForeColor.RGB = RGB(255, 0, 0)

注意:最好不要使用ActiveChart,它是“亲戚”。而是使用引用的对象。在这种情况下,请使用ChartObject

简单的参考代码:

Dim Chtobj                     As ChartObject

' modify "Chart_Data" Name to your Sheet, and "Chart 1" to your chart's name
Set Chtobj = Sheets("Chart_Data").ChartObjects("Chart 1")
With Chtobj
    ' modify the chartobject properties here...

    ' modify the major unit of X-axis
    .Axes(xlCategory).MajorUnit = 5 '<-- modify to whatever value you want
    ' modify the minor unit of X-axis
    .Axes(xlCategory).MinorUnit = 1
End With

答案 1 :(得分:1)

我不确定为什么边框没有显示,但我怀疑必须有一个属性来决定它的可见度或厚度。

要清理代码,请尝试以下操作:

Dim oChart as Object
ActiveSheet.Cells(10000, 10000).Select
Set oChart = ActiveSheet.Shapes.AddChart

With oChart' clear SeriesCollection
    Do Until .SeriesCollection.Count = 0
        .SeriesCollection(1).Delete
    Loop

    .ChartType = xlColumnClustered
    .PlotVisibleOnly = True
    .Location Where:=xlLocationAsNewSheet, Name:="Plottt"     ' rename chart sheets

' create SeriesCollection for each line
    .SeriesCollection.NewSeries
    With .SeriesCollection(1)
        .Name = "Hallo"
        .XValues = breaks
        .Values = freq
    End With

    With .ChartGroups(1)
        .GapWidth = 0
        With .Border
            .ColorIndex = 3
            ' You may need the LineStyle property of the border
            ' https://msdn.microsoft.com/en-us/library/office/ff821622.aspx
            .Linestyle = xlContinuous
        End With
    End With
End With ' Ends the with block for the entire chart

这不仅应该清理你的代码并使其更容易调试,但是如果我尝试在with块之外更新它的属性,那么我遇到的情况是我正在使用的对象无法正确更新。我不确定是否有理由这样做,但我在使用With块时犯错,以防万一。