Excel VBA打破子电话
我有一个名为“GFATMEN”的ChartObject,当我检查电子表格中的某些表单控件checkbox时,需要更新图例。 他们正在使用,所以我可以在图表上显示或不显示一些数据,而且我需要显示或不显示图例。
例如,当我点击一个checkbox时,我有这个子:
Private Sub MerT_Click() Application.ScreenUpdating = False On Error Resume Next ActiveSheet.ChartObjects("GFATMEN").Activate If ActiveSheet.SeriesCollection.Count = 3 Then With ActiveChart If MerT = False Then .SeriesCollection(3).Format.Line.Visible = msoFalse Call show_legend Else .SeriesCollection(3).Format.Line.Visible = msoTrue Call show_legend End If End With End If Application.ScreenUpdating = True End Sub
它调用另一个子show_legend
,重新创build图例并对其进行格式化:
Sub show_legend() ActiveSheet.ChartObjects("GFATMEN").Activate With ActiveChart .HasLegend = False .HasLegend = True .Legend.Position = xlLegendPositionBottom .Legend.Font.Name = "Tahoma" .Legend.Font.Size = 10 .Legend.Font.ForeColor.Brightness = 0.25 End With If MerT = False Then ActiveChart.Legend.LegendEntries.Item(3).Delete If MerE = False Then ActiveChart.Legend.LegendEntries.Item(2).Delete If MerI = False Then ActiveChart.Legend.LegendEntries.Item(1).Delete End Sub
问题是,代码总是从show_legend
子show_legend
中断,并且只要读取了.Legend.Font.ForeColor.Brightness = 0.25
行,就会返回到前一个子.Legend.Font.ForeColor.Brightness = 0.25
。 我已经把这一行放在前一节,在.HasLegend = True
行之后,同样的事情发生了。
我找不到任何与我的问题相关的答案。 谢谢。
你可以尝试像下面的代码:
With .Chart.Legend .Position = xlLegendPositionBottom .Font.Size = 10 .Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) '<-- modify the font color .Format.TextFrame2.TextRange.Font.Fill.ForeColor.Brightness = 0.25 '<-- modify the font brightness End With