2017-03-01 54 views
1

我找到了更新最大和最小比例圖表的方法。我調整了代碼以找到最後一行的最大比例值。下面是代碼:Excel VBA - 更新圖表比例中的錯誤

Sub ScaleAxes() 

Dim LastRow, LastRow2 As Long 

    With ActiveChart.Axes(xlCategory, xlPrimary) 
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    .MaximumScale = LastRow 
    .MinimumScale = ActiveSheet.Range("A2").Value 
    End With 
    With ActiveChart.Axes(xlValue, xlPrimary) 
    LastRow2 = .Cells(.Rows.Count, "B").End(xlUp).Row 
    .MaximumScale = LastRow2 
    .MinimumScale = ActiveSheet.Range("B2").Value 
    End With 
End Sub 

但是,我得到「對象不支持此屬性或方法」錯誤。我不知道哪部分我的代碼是錯誤的。希望你們能幫助我。

+0

哪行引發錯誤?您錯過了工作表引用的外觀 - 「.Cells」不是「Axes」類的屬性。 –

+0

LastRow = .Cells(.Row.Count ..... line是引發錯誤信息的那一行 – Jeeva

回答

0

.Cells不是Axes類的With聲明和中的屬性讀取你想要的時候,很可能你正試圖獲得該圖是在工作表的Cells屬性訪問的Axes該屬性。要到工作表的引用可以使用:

Dim ws As Worksheet 

Set ws = ActiveChart.Parent.Parent 

然後使用ws參考,當你得到LastRow值:

Option Explicit 

Sub ScaleAxes() 

    Dim LastRow, LastRow2 As Long 
    Dim ws As Worksheet 

    Set ws = ActiveChart.Parent.Parent 

    With ActiveChart.Axes(xlCategory, xlPrimary) 
     LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 
     .MaximumScale = LastRow 
     .MinimumScale = ActiveSheet.Range("A2").Value 
    End With 
    With ActiveChart.Axes(xlValue, xlPrimary) 
     LastRow2 = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row 
     .MaximumScale = LastRow2 
     .MinimumScale = ActiveSheet.Range("B2").Value 
    End With 

End Sub 
+0

代碼正在工作,但僅適用於第一個值但不是最後一個值(最大比例)的最小比例。下面的鏈接,你可以看到最初的圖: [鏈接] http://imgur.com/a/dCz92 因此,我需要使圖形全面的規模。這就是代碼的地方。上面的代碼管理設置最小刻度但不是最大刻度,如下圖所示: [鏈接] http://imgur.com/a/NIJqG – Jeeva

+0

無法理解您的代碼如何工作重新設置沒有源數據的軸等你的問題是關於修復一個完全不同的錯誤... –

+0

嗨羅賓。這裏是數據文件的鏈接https://wetransfer.com/dow nloads/c7fbe8ffd72778942d35c907fc78749d20170301064051/22f747924a806349a475b0ad83373f2420170301064051/e64cab – Jeeva