2
在VBA for Excel 2007中,我想將文本框添加到活動工作表並將其公式設置爲單元格。我的問題是AddTextbox
函數返回一個類型名爲Shape
而不是TextBox
的對象,因此它沒有Formula
屬性來設置。相反,我最終遍歷所有文本框以找到正確的文本框,然後設置其Formula
。有一個更好的方法嗎?插入文本框並設置公式
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