简体   繁体   English

Excel VBA打破Sub调用

[英]Excel VBA breaking from Sub call

I have a ChartObject named "GFATMEN" that needs to update the legend when I check some form control Checkboxes in a Spreadsheet. 我有一个名为“GFATMEN”的ChartObject,当我检查电子表格中的某些表单控件复选框时,需要更新图例。 They are being used so I can show or not some data on the chart, and I need the legend to be shown or not as well. 它们正被使用,因此我可以在图表上显示或不显示某些数据,我需要显示或不显示图例。

For example, I have this sub when a checkbox is clicked: 例如,单击一个复选框时,我有这个子:

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

It calls the other sub show_legend , that recreates the legend and formats it: 它调用另一个子show_legend ,重新创建图例并对其进行格式化:

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

The problem is that the code ALWAYS breaks from the show_legend sub an gets back to the previous sub whenever the line .Legend.Font.ForeColor.Brightness = 0.25 is read. 问题是,只要读取.Legend.Font.ForeColor.Brightness = 0.25行,代码ALWAYS就会从show_legendshow_legend断开并返回到前一个子.Legend.Font.ForeColor.Brightness = 0.25 I've already put this line on a previous section, right after the .HasLegend = True line, and the same thing happens. 我已经在.HasLegend = True行之后的前一部分放了这行,并且同样的事情发生了。

I couldn't find any answer that correlates to my problem. 我找不到任何与我的问题相关的答案。 Thank you. 谢谢。

You can try something like the code below: 您可以尝试类似下面的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM