2017-01-01 105 views
0

由以下代碼表示最大值。和Min。 vba中圖表的主水平X軸的縮放比例正在設置;二級水平X軸VBA的最大刻度

Sub chart_set() 
    Dim objCht As ChartObject 
    With ActiveSheet.ChartObjects(1).Chart.Axes(xlValue) 
    .MinimumScale = 0 
    .MaximumScale = my_max_var 
    End With 
End Sub 

有沒有什麼方法可以設置輔助功能的呢?

回答

1

首先,下面的代碼將Set objCht到ChartObject在ActiveSheet(看到這裏建議如何從ActiveSheetHow to avoid using Select in Excel VBA macros遠離)

下面的代碼將檢查是否應用了輔助軸,如果不將它添加到圖表。之後,它會將MinimumScaleMaximumScale修改爲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 
+0

感謝曬,只是我需要的東西! – Hamidreza

+1

Your'e welcome :)嘗試使用下一次'Sheets(「YourSheetName」)',而不是'ActiveSheet'。它將幫助您避免可能的錯誤 –