简体   繁体   English

如何使用 vba 使用公式将单元格内容插入图表文本框

[英]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.我正在使用 VBA 创建一个聚集条形图。 One of the features that I need is right-aligned Y-axis labels.我需要的功能之一是右对齐的 Y 轴标签。 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.不幸的是,这似乎是不可能的,并且(据我所知)更改 alignment 的唯一方法是不显示 Y 轴标签并插入文本框。 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.我需要使用公式来使用包含 Y 轴 label 的单元格填充文本框,但尝试执行此操作时不断出现错误。

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".我的宏在最后一行“Selection.Formula...”上失败,返回的错误消息是“无法设置 TextBox 类的公式属性”。 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不要认为有可以使用的FormulaR1C1属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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