取消选择标签和图表Excel VBA

取消选择标签和图表Excel VBA,vba,excel,charts,Vba,Excel,Charts,我使用“Sheet1”上的命令按钮使用VBA创建图表,但该图表正在添加到另一张图纸(“Sheet2”) 添加图表后,我使用以下代码根据DataLabel值为条形图着色,并更改DataLabel: Dim oPoint As Excel.Point Dim sngPercente As Single For Each oPoint In Worksheets("Sheet2").ChartObjects("Performance").Chart.Serie

我使用“Sheet1”上的命令按钮使用VBA创建图表,但该图表正在添加到另一张图纸(“Sheet2”)

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

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
不工作,因为代码正在从另一个工作表运行

ActiveChart.Deselect
根本不起作用

删除
oPoint.DataLabel。从代码中选择

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

SendKeys "{ESC}"
工作正常,但非常不可靠,就像与其他宏一起使用一样,它将中断代码,这将导致“代码执行已中断”错误


还有什么我可以试试的吗?

这里有一个小小的矛盾

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

我注意到在您的代码中,工作表被命名为“sheet 2”,并带有空格。您是否试图在工作表中选择一个不存在的单元格?

我可以通过读取值而不是标题来完全避免此问题:

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
Dim ChartRng As范围
Dim ser作为Excel.Series
Set ChartRng=工作表(“概述”).范围(“A1:C19”)
Dim oChtObj作为图表对象
设置oChtObj=Worksheets(“Overview”).ChartObjects.Add(左:=48,宽:=570,顶:=1000,高:=367)
用oChtObj.图表
.Parent.Name=“性能”
.ChartType=xlColumnClustered
.ApplyLayout(1)
.SetSourceData图表
.HasLegend=True
设置ser=.SeriesCollection(1)
ser.HasDataLabels=True
.SeriesCollection(2).HasDataLabels=False
.hasttle=正确
.ChartTitle.Caption=“每个代理面临呼叫的时间(KPI:75%)
.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).删除
.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(217217217217)
.Axes(xlValue).MajorGridlines.Border.Color=RGB(217217217217217)
以
设置oChtObj=Nothing
将oPoint设置为Excel.Point
单件尺寸SNG百分比
与ser
对于n=1到.Points.Count
设置oPoint=.Points(n)
sngPercente=.value(n)*100
带点
如果sngPercente<70,则
.Interior.Color=RGB(255,0,0)
如果结束
如果sngPercente>75,则
.Interior.Color=RGB(0,176,80)
如果结束

如果sngPercente>=70且sngPercente,如果删除
oPoint.DataLabel.选择
行,会出现什么错误?请检查:不,这实际上是发布问题时的打字错误。我把它修好了,但我的意思是这不起作用。整洁优雅!简直完美。谢谢。
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