Geographos
Geographos

Reputation: 1466

VBA Excel how to write Excel formula in the textbox

How can I put the Excel formula into the textbox?

I tried sth like this:

 Sub CivBox()
  With ActiveSheet
   .Shapes("Civils 3").Copy
   [C26].Activate
   .Paste
   .Shapes(.Shapes.Count).Name = "Civils 4"
   .Shapes("Civils 4").TextFrame2.TextRange.Characters.Text = "=D51"
 End With
End Sub

But the only the string is returned

enter image description here

Is it possible to put the Excel formula into the textBox or shall I prepare it in some cell first, and next link this cell into this textbox as a range?


Upvotes: 2

Views: 1329

Answers (2)

SJR
SJR

Reputation: 23081

If you're using a Shape you could use this

Sub x()

ActiveSheet.Shapes(1).OLEFormat.Object.Formula = "=A1"

End Sub

For a textbox you can use

ActiveSheet.TextBoxes("TextBox 1").Formula = "=A1"

Upvotes: 5

Mech
Mech

Reputation: 4015

You can do this indirectly. You can't put it IN the box, but you can select the textbox (not the contents of the textbox), go to the formula bar, and reference a cell.

Edit: This might help: https://www.solveyourtech.com/how-to-display-a-formula-result-in-a-text-box-in-excel-2010/

Upvotes: 1

Related Questions