user1681664
user1681664

Reputation: 1811

vba charting in excel 2010

I am just trying to chart a simple series, but I get some weird error when I try to label my axis. How can I label my x,y axis?

With ActiveSheet.ChartObjects.Add(Left:=10, Width:=875, Top:=75, Height:=425)
    .Chart.SetSourceData Source:=ws.Range("A1:B" & rows)
    .Chart.ChartType = xlLine
        With ActiveChart
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Price"
        End With
End With

The error is: Object variable or with block variable not set

Upvotes: 0

Views: 462

Answers (1)

Automate This
Automate This

Reputation: 31364

This worked for me, try removing the with block within the other with:

ActiveSheet.Shapes.AddChart(Left:=10, Width:=875, Top:=75, Height:=425).Select

With ActiveChart
    .SetSourceData Source:=ws.Range("A1:B" & rows)
    .ChartType = xlLine
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Time"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Price"
End With

Upvotes: 2

Related Questions