Link to home
Start Free TrialLog in
Avatar of Andreas Hermle
Andreas HermleFlag for Germany

asked on

Add textboxes to charts

Dear Experts:

below macro adds a text box to the first chart of the active worksheet. The contents for  the text box is taken from a cell that has been named Blue_Range_TextBox_01.

There are seven charts on the active worksheet and seven text boxes to be added.
The contents for these textboxes have to be taken from cells named ...
Blue_Range_TextBox_01
Blue_Range_TextBox_02
Blue_Range_TextBox_03
...
Blue_Range_TextBox_07

So, my requirements for the macro is as follows:

The code is to loop thru all the names (Blue_Range_TextBox_xx) and add a text box to each and every chart with the Chart number (Chart.Objects(1 or 2 or 3 etc.)) corresponding with the number at the end of the named ranges (Blue_Range_Text_Box_01 or _02 or _03 etc.).

I hope I could make myself clear.

Help is much appreciated. Thank you very  much in advance.

I have attached a sample file (with below code included) for your convenience.

Again, thank you very much for your professional help.

Regards, Andreas

 SampleFileTextBoxes.xlsm
Sub TextBox_Add()

Dim chtTemp As Chart
Dim objTemp As Object

Set chtTemp = ActiveSheet.ChartObjects(1).Chart
Set objTemp = chtTemp.Shapes.AddTextBox(msoTextOrientationHorizontal, 250, 174, 140, 20)

objTemp.Select
Selection.Formula = "='" & chtTemp.Parent.Parent.Name & "'!Blue_Range_TextBox_01"

ActiveCell.Select

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of aszabo1
aszabo1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andreas Hermle

ASKER

Dear asZ,

thank you very much for your swift response.  I guess, we are almost there

Thank you very much, It WORKS as desired, BUT as a matter of fact, the macro is to repeat any times relative to the number of charts and the corresponding numbers of named ranges. The sample file with 7 charts and named ranges is just an example.

ie. there could be 20 named ranges and corresponding 20 charts or fewer or more (any number).

IN CASE of say 20 named ranges, the ranges would be named as follows:
Blue_Range_TextBox_02
Blue_Range_TextBox_03
etc
Blue_Range_TextBox_20

The 20 charts are named as well (mychart01 till mychart20)
If you select the charts in the sample file the chart names (mychart01 etc.) appear in the name box.

With english not being my mother tongue, I hope I could make myself clear.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Hi asZ,

I managed to tweak your code so that it suits my needs. Please have a look at the below code.

There is one thing I would like to know.

As I told you my charts have been named 'mychart01, mychart02, and so forth by running another macro  (code line in the other macro is: myChtObj.Name = "mychart" & Format(i + 1, "00")).

Hence my new requirement for below code is as follows:

Only charts named 'mychart01, mychart02 ... mychartxx are to be affected by this macro. Other charts (unnamed ones or charts that have been named differently) are to be left untouched by this macro.

So is it possible for you to rewrite below macro to comply with the above (new) requirement.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Sub TextBox_Add_2()
'your code tweaked/slightly altered by me

Dim chtTemp As Chart
Dim objTemp As Object
Dim x As Integer

x = 1
Do While x <= ActiveSheet.ChartObjects.Count 'added by me
Set chtTemp = ActiveSheet.ChartObjects(x).Chart
Set objTemp = chtTemp.Shapes.AddTextBox(msoTextOrientationHorizontal, 250, 174, 140, 20)

objTemp.Select
Selection.Formula = "='" & chtTemp.Parent.Parent.Name & "'!Blue_Range_TextBox_" & Format(x, "00") 'tweaked by me
x = x + 1
Loop

ActiveCell.Select

End Sub

Open in new window

Hey ASz,

thank you very much for your professional help. I really appreciate it.

Regards, Andreas