excelvba

Double TextBox in Shape using DrawingObject.Formula


When my macro runs, it links values in some cells, then I use those cell values in shapes with DrawingObject.Formula. This way I can only get to the shape to look like the right square, but i would need it to have the second value right below it(see image)

Range("E1").Value = Data
Var1 = Range("E2").Value
Var2 = Range("E3").Value
Var3 = Range("E5").Value
Var4 = Range("F7").Value
Var5 = Range("F8").Value
Var6 = Range("F9").Value
Var7 = Range("E8").Value


wsLP.Shapes(ArrayColBloco(0)).DrawingObject.Formula = "=E1"
wsLP.Shapes(ArrayColBloco(1)).DrawingObject.Formula = "=E2"
wsLP.Shapes(ArrayColBloco(2)).DrawingObject.Formula = "=E3"
wsLP.Shapes(ArrayColBloco(3)).DrawingObject.Formula = "=E5"
wsLP.Shapes(ArrayColBloco(4)).DrawingObject.Formula = "=F5"
wsLP.Shapes(ArrayColBloco(5)).DrawingObject.Formula = "=F6"
wsLP.Shapes(ArrayColBloco(6)).DrawingObject.Formula = "=F7"
wsLP.Shapes(ArrayColBloco(7)).DrawingObject.Formula = "=E8"

Square shapes with the correct format in the left and the wrong (the one that im making with the macro) on the right

Probably would need another way to link the cell values to the shapes textbox


Solution

  • o It is not possible to put a formula as source to a shape that contains more than a single cell reference, see Text And Formula In Shape Text. So something like "=E2" & char(10) & F2" will fail - in Excel, you get an error message "The formula is missing a cell reference...". If you try this with code, you get a runtime error 1004 "Unable to set the Formula property..."

    o You could use a helper cell (maybe on a hidden sheet or a hidden column) that has the formula, and reference that cell.

    Formula in Z2: =E2 & char(10) & F2
    Formula for the shape: =Z2

    o When you use cell references as content for a shape, you can't format the characters individually, it's an "all or nothing". So you can't have the first part in bold+red and the second part in black font as you show in your picture.

    o I would advice to simply use 2 shapes - put a small shape inside the shape you have, make it transparent (and no border, but do this only after you are sure everything works as it is hard to find an invisible shape on top of another).

    enter image description here