views:

108

answers:

3

Hi, I'm trying to make a macro that formats a chart in Excel 2003 where the data changes. Basically, I have a 20 X values and Y values at all times; however, the values are data specific (I'm making stock price charts that will change depending on the stock I'm analying). I'm trying to make my Y-Axis cross the X axis at the value in cell B8; is there anyway to do this with a macro? Because I can't link where the axes cross to a cell. Also, I want to change the axis minimum to cell B8 as well. Also I want the macro to adjust the cart to look logical automatically depending on the data I put in there (ie logical intervals).

The chart type here is a Scatter plot, where the desription is: "Scatter with Data Points Connected by Lines Without Markers". Thank you very much.

A: 

This is approximately what you need (no time here to test and get the details exact):

ActiveChart.Axes(xlValue).CrossesAt = Range("B8").value

You might also have to set

ActiveChart.Axes(xlCategory).Crosses = xlAxisCrossesCustom

and play around a little with whether to use Value or Category.

Lance Roberts
A: 

I don't think it's possible to dynamically link the intercept value to a cell - this is just based on the fact that the UI for selecting the intercept value requires an explicit value, rather than allowing you to select a cell.

Within VBA, however, once you have read the desired value from the cell, do

ActiveSheet.ChartObjects("Chart 1").Axes(xlValue).CrossesAt = value

(with your chart name)

AakashM
A: 

"to adjust the cart to look logical automatically depending on the data I put in there (ie logical intervals)."

That one is a lot of fun. Here's a VBA function that does the hard part of calculating a pretty interval between the ticks.

Public Function prettyVal( _
    xMin As Double, _
    xMax As Double, _
    minBins As Integer) _
    As Double
'' returns an aesthetic interval size to _
    use for a plot axis or histogram bin. _
    [email protected] 2010-09-01

    Dim pretties
    pretties = Array(1, 2, 5, 10)
    Dim maxBin As Double ''maximum size of bin
    Dim xScale As Double  ''scale factor

    With WorksheetFunction
    maxBin = (xMax - xMin) / minBins
    xScale = 10 ^ Int(.Log10(maxBin))
    prettyVal = xScale * .Lookup(maxBin / xScale, pretties)
    End With
End Function

You'll want to use it in a worksheet. Use the floor and ceiling of the min and max with the pretty value for significance. This makes them also pretty. Something like this in the worksheet:

minimum plot value   minVal     120     
maximum plot value   maxVal     980     
minimum num of bins  minBins     10     
pretty bin size      binsize     50   =prettyVal(minVal,maxVal,minBins)
low axis value       minEdge    100   =FLOOR(minVal,binsize)
high axis value      maxEdge   1000   =CEILING(maxVal,binsize)
number of bins       numBins     18   =(maxEdge-minEdge)/binsize

Enjoy.

Marc Thibault