View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jon Peltier Jon Peltier is offline
external usenet poster
 
Posts: 6,582
Default Check for named shape in chart


"Fredrik E. Nilsen" wrote in message
...
Hi,

If I have understood it correctly, there is no way to control the
placement of the Axis title object in a chart or control the text line
breaks in it through VB.


You can easily get or set the position of an axis title:

Sub AxisTitlePlacement()
If ActiveChart.HasAxis(xlValue, xlPrimary) Then
If ActiveChart.Axes(xlValue, xlPrimary).HasTitle Then
With ActiveChart.Axes(xlValue, xlPrimary).AxisTitle
' get axis title position
MsgBox "Axis Position Left: " & .Left & ", Top: " & .Top
' set axis title position
.Left = 0
.Top = ActiveChart.PlotArea.Height / 2 +
ActiveChart.PlotArea.InsideTop - 12
End With
End If
End If
End Sub

Line breaks are not as straightforward. You cannot make the line any longer
than Excel decides (other than by shrinking the text or stretching the
chart), but you could insert hard breaks. You would have to write code smart
enough to read the text

MyText = ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text

and decide where a break should be.

Text boxes are in fact the way to get around the limitation you have in
resizing the title box.

I'm building a code to apply user defined settings on charts and the
code adds a textbox in stead of using the Axis title so the placement
can be controlled and the user can control the line breaks.

The problem is: I the user applies one user defined chart and then
change to another, the text box is added again. I'm sure there is a
way to check if a named text box exist in a chart, and add it if it
doesn't. This is the current code:


This procedure looks for a textbox with a particular name. If it finds it,
it updates the text. If it doesn't find it, it adds a new one, names it, and
adds text.

Sub UpdateOrAddTextBox()
Dim shp As Shape
Dim bTitleFound As Boolean
If ActiveChart.Shapes.Count 0 Then
For Each shp In ActiveChart.Shapes
If shp.Name = "textbox_YAxisTitle" Then
' we found it
bTitleFound = True
' adjust text and position of textbox
shp.TextFrame.Characters.Text = "New Y Axis Title"
End If
Next
End If
If Not bTitleFound Then
' didn't find it, so add it
Set shp = ActiveChart.Shapes.AddTextbox(msoTextOrientationUp ward, 0, _
ActiveChart.PlotArea.Height / 2 + ActiveChart.PlotArea.InsideTop -
12, 20, 100)
With shp
.Name = "textbox_YAxisTitle"
.TextFrame.Characters.Text = "Y Axis Title"
.TextFrame.AutoSize = True
End With
End If
End Sub

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______