gpt4 book ai didi

excel - 插入文本框并设置公式

转载 作者:行者123 更新时间:2023-12-03 02:04:42 24 4
gpt4 key购买 nike

在 Excel 2007 的 VBA 中,我想将文本框添加到事件工作表并将其公式设置为单元格。我的问题是 AddTextbox 函数返回一个类型名为 Shape 的对象,而不是 TextBox,因此它没有 Formula code> 要设置的属性。相反,我最终循环遍历所有文本框以找到正确的文本框,然后设置其公式。有更好的方法吗?

Sub insertTextBoxWithFormula()
Set newTextBox = ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 200, 200, 150, 150)
newTextBox.Name = "New TextBox"
'newTextBox.Formula = "=$A$1" 'This is what I wanted to do

'This is what I did instead
For Each CurrentTextBox In ActiveSheet.TextBoxes
If CurrentTextBox.Name = "New TextBox" Then
CurrentTextBox.Formula = "=B3"
CurrentTextBox.Name = "Finished TextBox"
End If
Next CurrentTextBox
End Sub

最佳答案

您可以使用控件的名称对 TextBoxes 集合进行索引。所以你的例子可以缩写为:

ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 200, 200, 150, 150).Name = "New TextBox"
ActiveSheet.TextBoxes("New TextBox").Formula = "=$B$3"

关于excel - 插入文本框并设置公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23772309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com