Alaa Elwany
Alaa Elwany

Reputation: 697

Position Chart relative (over or under) another chart in VBA

I have a sheet with an chart in it. I am using another sub to create another chart, and would like to place it UNDER the first chart.

I know how to set the position of a chart using .Top or .Left, but don't know how to retrieve the position of the first chart. Any suggestions?

I tried:

Activesheet.ChartObjects(2).Top = Activesheet.ChartObjects(1).Top

--> This works just fine, but the charts are overlapped. I need to replace the last ".Top" with ".Bottom", but ".Bottom" is not accepted (In fact, optimally, I would like to place at the bottom of the first chart with a little gap).

Sounds easy, but can't quite do it!

Thanks, Al

Upvotes: 2

Views: 4342

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149295

Is this what you are trying?

Option Explicit

Sub Sample()
    Dim C1CO As ChartObject, C2CO As ChartObject

    Set C1CO = ActiveSheet.ChartObjects("Chart 1")
    Set C2CO = ActiveSheet.ChartObjects("Chart 2")

    With C2CO
        .Top = C1CO.Top + C1CO.Height + 10
        .Left = C1CO.Left
    End With
End Sub

Upvotes: 3

Related Questions