VBA Y Axis Adjustment - VBA only works on 1 sheet?

trrbrg

New Member
Joined
Dec 23, 2015
Messages
9
Hi!

I've pieced together some VBA from the internet to adjust the Y axis based on the calculated results of specific cells. The VBA works wonderfully for the chart on the first tab.

The problem is, when I copy and paste the VBA to the next sheet, adjust the cell references and the chart name, it doesn't work for the second chart. It adjusts the axis, but incorrectly.

This is the VBA that I am using:

Option Explicit
Private Sub Worksheet_Calculate()
Dim cht As Chart
Dim wks As Worksheet


Set wks = ActiveSheet
Set cht = wks.ChartObjects("Chart District").Chart


If wks.Range("$H$2").Value <> cht.Axes(xlValue).MaximumScale Then
cht.Axes(xlValue).MaximumScale = wks.Range("$H$2").Value
End If
If wks.Range("$I$2").Value <> cht.Axes(xlValue).MinimumScale Then
cht.Axes(xlValue).MinimumScale = wks.Range("$I$2").Value
End If
If wks.Range("$J$2").Value <> cht.Axes(xlValue).MajorUnit Then
cht.Axes(xlValue).MajorUnit = wks.Range("$J$2").Value
End If
End Sub

Does anyone have any ideas as to why this won't work on multiple tabs?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top