Question

I'm trying to insert a chart into a worksheet that would display information from 4 columns.

The x-axis has the dates and the y-axis would have a $ figure amount. This is what I've gotten from recording a macro (which shows what I want at least):

ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""target"""
ActiveChart.SeriesCollection(1).XValues = "=MacroParty!$R$4:$R$55"
ActiveChart.SeriesCollection(1).Values = "=MacroParty!$S$4:$S$55"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = "=""current"""
ActiveChart.SeriesCollection(2).XValues = "=VA05NDump!$G$2:$G$833"
ActiveChart.SeriesCollection(2).Values = "=VA05NDump!$P$2:$P$833"

The problem is that this just plops a chart int he middle of the sheet and I'm looking to have it in a specific location.

I'm guessing there's a way nicer way of writing this but I am new to VBA and relying on the brute force method.

Any help would be greatly appreciated!

Was it helpful?

Solution

OK you can clean this up a bit. The macro recorder is great because it can help you see what you need to do, but it is little more than simply replicating key-strokes or mouse-clicks, and the point of programming in VBA should be to work directly with the objects -- much more efficiently than mimicking keystrokes.

I will show some examples of using variables to make your code more concise and hopefully easier to interpret. I'll comment it for you so you can see what I'm doing, and why.

Sub Test()
Dim cObj As Shape  'A Shape container for the Chart
Dim cht As Chart   'Variable will represent the chart
Dim srs As Series  'Variable will represent series

    Set cObj = ActiveSheet.Shapes.AddChart   '##cObj will refer to this shape
    Set cht = cObj.Chart                     '## cht will refer to this chart

    '(Now, the variable "cht" will refer to this specific chart until/unless
    '  you assign it to another chart.

    '## Set the chart type:
        cht.ChartType = xlXYScatterSmoothNoMarkers

    '## To manipulate the chart's size and location you can use
    '   something like this to align it with a cell
    '   there are also Height, Top, Left, Width, etc.
        cObj.Top = Range("A1").Top
        cObj.Left = Range("A1").Left

    '## To manipulate it's size, you can work with
    ' there are also Height, Top, Left, etc. properties, etc.
        cObj.Width = 500
        cObj.Height = 300

    '## Manipulating the PlotArea, etc.
    ' there are also Height, Top, Left, etc. properties, etc.
        cht.PlotArea.Width = 450
        cht.PlotArea.Height = 250

    '## Add series to the chart

    Set srs = cht.SeriesCollection.NewSeries
        srs.Name = "=""target"""
        srs.XValues = "=MacroParty!$R$4:$R$55"
        srs.Values = "=MacroParty!$S$4:$S$55"

    '## "srs" will refer to SeriesCollection(1) until/unless you 
    '    assign it to something else...

    '... like now, we want to add another series, 
    '  so we will just assign another NewSeries to the "srs" variable
    ' and work with the variable srs.
    Set srs = cht.SeriesCollection.NewSeries
        srs.Name = "=""current"""
        srs.XValues = "=VA05NDump!$G$2:$G$833"
        srs.Values = "=VA05NDump!$G$2:$G$833"
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top