PDA

View Full Version : Solved: positioning a Chart



leal72
09-22-2009, 03:03 PM
Is it possible for the Chart to always be positioned in the center of a worksheet? If so, how?

I have a chart that is on the same sheet as the data but it gets resized when columns are added or removed. I'd like to keep it centered or at least keep it from getting resized if rows/column are added or removed.

Bob Phillips
09-22-2009, 03:42 PM
Try this



Option Explicit

Private mHeight As Double
Private mWidth As Double
Private mTop As Double
Private mLeft As Double
Const THIS_CHART As String = "myChart" '<<<<< change to chart name

Private Sub Worksheet_Change(ByVal Target As Range)
With Me.ChartObjects(THIS_CHART)

.Height = mHeight
.Width = mWidth
.Top = mTop
.Left = mLeft
End With

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Me.ChartObjects(THIS_CHART)

mHeight = .Height
mWidth = .Width
mTop = .Top
mLeft = .Left
End With
End Sub


This is worksheet event code, which means that it needs to be
placed in the appropriate worksheet code module, not a standard
code module. To do this, right-click on the sheet tab, select
the View Code option from the menu, and paste the code in.

leal72
09-23-2009, 06:41 AM
Thank you! works great!