简体   繁体   中英

How to use vba to use formula to insert cell contents into a chart textbox

I am using VBA to create a clustered bar chart. One of the features that I need is right-aligned Y-axis labels. Unfortunately this does not appear to be possible and (as I understand it) the only way to change the alignment is to not show the Y-axis labels and insert text boxes. I need to use a formula to populate the text box with the cell that contains the Y-axis label, but I keep getting an error when trying to do this.

I used the record macro feature to generate code that I cleaned up a little bit to yield:

'Y axis label corresponding to cell A12 
co.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 0, 35, 225, 30).Select
co.Chart.Shapes.Range(Array("TextBox 2")).Select
Selection.Formula = "='Figure 1'!R12C1"

My macro fails on the last line "Selection.Formula..." and the error message returned is "Unable to set the Formula property of the TextBox class". Any suggestions for the code i need to make this work?

For example:

Dim TB, cht

Set cht = ActiveSheet.ChartObjects(1).Chart

Set TB = cht.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 10, 80, 15)

TB.OLEFormat.Object.Formula = "=A12"

Don't think there's a FormulaR1C1 property you can use

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM