【问题标题】:Deselect Label And Chart Excel VBA取消选择标签和图表Excel VBA
【发布时间】:2014-08-15 13:06:05
【问题描述】:

我正在使用“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

运行此代码并转到“Sheet2”后,我注意到图表和其中的最后一个数据标签仍处于选中状态。


(来源:gulfup.com

如何取消/取消选择此图表?

这是我尝试过的:

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

无法工作,因为代码是从另一个工作表运行的。

ActiveChart.Deselect

根本不工作。

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

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

SendKeys "{ESC}"

有效,但非常不可靠,好像与其他宏一起使用它会破坏代码,这将给出“代码执行已被中断”的错误。

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

【问题讨论】:

标签: vba excel charts


【解决方案1】:

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

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

【讨论】:

  • 整洁优雅!简直完美。非常感谢。
【解决方案2】:

这里有个小矛盾

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

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

【讨论】:

  • 不,实际上是发布问题时的拼写错误。我修复了它,但我的意思是这不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
相关资源
最近更新 更多