MG78
MG78

Reputation: 219

Excel VBA - Error in Updating Maximum Scale for XY Scatter Chart

I have a code where it need to change the scale of chart when user click a button. However the code is not working for the maximum scale which is the last used row. Below is the code:

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

End Sub

In the picture below is the generated graph from my data which is Power vs Frequency. The Frequency range is 1.12 GHz to 1.45 GHz.

enter image description here

After i apply the code above, the chart changed as below:

enter image description here

As you can see in the picture, the minimum scale which is 1.12 GHz is working but not for the maximum scale. Are there anything wrong with the code?

Thank you very much.

Upvotes: 2

Views: 286

Answers (1)

Pᴇʜ
Pᴇʜ

Reputation: 57683

The problem is that LastRow is the number of the last row. I assume you wanted the value of the last cell.

.MaximumScale = ws.Cells(LastRow, 1).Value

Upvotes: 1

Related Questions