你可以遍歷圖表對象,以顯示或隱藏所有的人,以節省時間和打字:
Option Explicit
Sub HideAllChartsInSheet1()
Dim oChartObject As ChartObject
For Each oChartObject In Sheet1.ChartObjects
oChartObject.Visible = False
Next
Set oChartObject = Nothing
End Sub
將Sheet1
替換爲您的實際Worksheet對象的引用,當然使用oChartObject.Visible = True
來顯示ChartObjects。
EDIT: 讓我們改變子修改特定圖表對象:
Option Explicit
Sub ChartVisible(psName As String, pbVisible As Boolean)
Dim oChartObject As ChartObject
For Each oChartObject In Sheet1.ChartObjects
If UCase(oChartObject.Name) = UCase(psName) Then
oChartObject.Visible = pbVisible
End If
Next
Set oChartObject = Nothing
End Sub
實施例:使用以下方法來隱藏 「圖4」 Sheet 1上:
Call ChartVisible("Chart 4", False)
我得到一個「對象變量或塊變量未設置」錯誤messgae – Niva
您是否在運行代碼之前激活了圖表? –
yup!我確實通過點擊來激活它 – Niva