Search code examples
vbaexcelaxes

Conditional minimum axes value for excel chart


I am looking for some VBA code which can automate the fixed minimum value of the x axis in a chart, depending on the earliest detected date and assigning one of a number of predefined values.

I am dealing with large quantities of data wherein the start date of the available data often varies.

Is it possible to detect the earliest year value in the x-axis and apply a predetermined start date?

For example, if data is available from the 4th May 2002, the code will read the minimum value in the x-axis range as 04/05/2002, detect the '2002' and set the minimum x-axis value to '37258' to correspond with the 1st January 2002 in Excel's date numbering system.

If data is available from mid-2008, again it would set the minimum value in the x-axis to '39449' to fit with 01/01/2008.


Solution

  • ActiveChart.Axes(xlValue).MinimumScale = dateserial(year(ActiveChart.Axes(xlValue).MinimumScale),1,1)
    

    This looks at the chart, takes the year from that, and then sets the start to that 1/1 of that year.