Tim.
Nice. I'm glad I was wrong about being able to change the x-axis min/max - it'll come in handy
The 'Worksheet_Change' event tells you the Range that has been changed by passing you the 'Target' Range Object
Private Sub Worksheet_Change(ByVal Target As Range
The 'Worksheet_Calculate' event doesn't
Private Sub Worksheet_Calculate(
A lot of the code you've pasted below is to identify whether the cells of interest have changed. I think your solution could be simpler - this should work
Option Explici
Private Sub Worksheet_Calculate(
ActiveWorkbook.Charts(1).Chart.Axes(xlCategory).MinimumScale = Range("$H$42").Valu
ActiveWorkbook.Charts(1).Chart.Axes(xlCategory).MaximumScale = Range("$H$43").Valu
End Su
If you ever insert/delete rows or columns in the worksheet, the correct cell references may no longer be $H$42 and $H$43. It's good practice to give names to the two cells and use them in your code to make it more robust, e.g.
Option Explici
Private Sub Worksheet_Calculate(
ActiveWorkbook.Charts(1).Chart.Axes(xlCategory).MinimumScale = Range("MINSCALE").Valu
ActiveWorkbook.Charts(1).Chart.Axes(xlCategory).MaximumScale = Range("MAXSCALE").Valu
End Su
As a final thought, this code will execute every time the worksheet recalculates, so you could remember the minimum and maximum values and only change the chart if necessary, e.g.
Option Explici
Private m_sgMin as Singl
Private m_sgMax as Singl
Private Sub Worksheet_Calculate(
If Range("MINSCALE").Value <> m_sgMin The
m_sgMin = Range("MINSCALE").Valu
ActiveWorkbook.Charts(1).Chart.Axes(xlCategory).MinimumScale = m_sgMi
End I
If Range("MAXSCALE").Value <> m_sgMax The
m_sgMax = Range("MAXSCALE").Valu
ActiveWorkbook.Charts(1).Chart.Axes(xlCategory).MaximumScale = m_sgMa
End I
End Su
Regards
JB
-----
[email protected] wrote: ----
JB
Thanks for your post, here is what I have so far..
Option Explici
Private Sub Worksheet_Calculate(
Dim target As Rang
Select Case target.Addres
Case "$H$42
ActiveWorkbook.Charts(1).Chart.Axes(xlCategory)
.MinimumScale = target.Valu
Case "$H$43
ActiveWorkbook.Charts(1).Chart.Axes(xlCategory)
.MaximumScale = target.Valu
Case Els
End Selec
End Su
The guts for this code come from Jon Peltier's web site
but his example uses the Change event versus the Calculat
event. Right now it bombs on the Select Case line
probably due to the Dim statement, but I am at a loss
Regards
Ti
-----Original Message----
Hi Tim
worksheet calculates, and will trigger your code when yo
want it tobut I don't think you can do it for the x-axis (I might b
wrong)to calculate the first row and last row of the data yo
want to plot (assuming the values are in columns). Th
MATCH function would probably do the jobfirst and last rows to plot are calculated in cells E1 an
F1, your code could look something like this
iLastRow = Sheet1.Range("F1").Valu
ActiveChart.SetSourceDat
Source:=Range(Sheet1.Cells(iFirstRow, 1)
Sheet1.Cells(iLastRow, 2)), PlotBy:=xlColumn