davideghz
davideghz

Reputation: 3685

Excel VBA - append formatted text to cell

I need to add a "padding-bottom" to a cell in VBA.

A way to get what I need might be to append to a cell a char with smaller font size (let's say a small dot) in a new line.

How can I achieve this in VBA?

Upvotes: 0

Views: 823

Answers (1)

jmdon
jmdon

Reputation: 1017

The following will insert "my text" into range A1, you can adapt it as you need.

It works as follows:

  1. Get the current number of characters in Range A1 (lOldTextLen)
  2. Insert two line breaks and "my text" into Range A1
  3. Update the characters in Range A1 to font size 8, starting from the end of the previous text (using lOldTextLen)

    Dim sText As String 
    Dim lOldTextLen As Long
    
    sText = "my text"
    lOldTextLen = Len(Range("a1"))
    
    Range("a1").Value = Range("a1").Value & vbNewLine & vbNewLine & sText
    
    Range("a1").Characters(lOldTextLen + 1).Font.Size = 8
    

Upvotes: 1

Related Questions