2017-01-31 44 views
1

我有一個名爲「GFATMEN」的ChartObject,當我檢查電子表格中的某些表單控件複選框時,需要更新圖例。他們正在使用,所以我可以在圖表上顯示或不顯示某些數據,並且我需要顯示或不顯示圖例。Excel VBA打破子電話

例如,我有這樣子被點擊複選框時:

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,即再現了傳奇和格式是:

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中斷開,只要讀取行.Legend.Font.ForeColor.Brightness = 0.25就返回到前一個子代。我已經把這一行放在前一節,緊接在.HasLegend = True行之後,同樣的事情發生了。

我找不到與我的問題相關的任何答案。謝謝。

+0

的複選框單純和梅里都類似潛艇到梅特。 –

+1

你從哪裏得到該代碼?沒有'Font.ForeColor'屬性。 – Rory

+0

這行代碼可能會拋出一個錯誤,因爲您在'MerT_Click'子文件中有'On Error Resume Next',因此將被忽略。 – Socii

回答

0

你可以嘗試像下面的代碼:

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