VBA中的列图表周围的黑色边框

我尝试从VBA代码的Column图表周围设置黑色边框。 这是我现在所拥有的。 最后一行,我设置Border.ColorIndex显然不工作。 目前这个列看起来像这样。

在这里输入图像说明

我希望它看起来像这样。

在这里输入图像说明

这是我的代码。

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 

另外我想从我的代码减less步长。 帮助这个也不胜感激。

要设置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 

我不知道为什么边界没有显示,但我怀疑必须有一个属性,决定其知名度或厚度。

为了清理你的代码,试试这个:

 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 

这不仅可以清理你的代码,而且可以更容易地进行debugging,但是如果我尝试更新它的属性以外的块,那么我正在使用的对象不能正确更新。 我不确定是否有这个原因,但是为了防万一,我错误地使用了With块。