首先,下面的代碼將Set objCht
到ChartObject在ActiveSheet
(看到這裏建議如何從ActiveSheet
How to avoid using Select in Excel VBA macros遠離)
下面的代碼將檢查是否應用了輔助軸,如果不將它添加到圖表。之後,它會將MinimumScale
和MaximumScale
修改爲20和50(只是我選擇的隨機數字)。
代碼
Option Explicit
Sub chart_set()
Dim objCht As ChartObject
Dim my_max_var As Long
' just for my tests
my_max_var = 1000
' set chart object
Set objCht = ActiveSheet.ChartObjects(1)
With objCht
With .Chart.Axes(xlValue, xlPrimary)
.MinimumScale = 0
.MaximumScale = my_max_var
End With
' check if Secondary Axis is applied
If .Chart.HasAxis(xlValue, xlSecondary) = False Then
.Chart.HasAxis(xlValue, xlSecondary) = True
End If
' modify minimum and maximum values of seconday axis
With .Chart.Axes(xlValue, xlSecondary)
.MinimumScale = 20
.CrossesAt = 20
.MaximumScale = 50
End With
End With
End Sub
感謝曬,只是我需要的東西! – Hamidreza
Your'e welcome :)嘗試使用下一次'Sheets(「YourSheetName」)',而不是'ActiveSheet'。它將幫助您避免可能的錯誤 –