Mennan
Mennan

Reputation: 4497

How to set specific text as bold via VBA

I want to set bold some text in string. How can I do it?

Upvotes: 12

Views: 97531

Answers (4)

GSerg
GSerg

Reputation: 78210

By using Characters.

Range("A1").Characters(8, 5).Font.Bold = True

Upvotes: 23

Natan C
Natan C

Reputation: 21

Try this if you need to bold multiple specific text:

Sub Find_and_Bold()
Dim rCell As Range, sToFind As String, iSeek As Long
Dim Text(1 To 4) As String
Dim i As Integer


Text(1) = "text1"
Text(2) = "text2"
Text(3) = "text3"
Text(4) = "text4"


For Each rCell In Range("C7:C1000")
    For i = LBound(Text) To UBound(Text)
            sToFind = Text(i)
            iSeek = InStr(1, rCell.Value, sToFind)
        Do While iSeek > 0
            rCell.Characters(iSeek, Len(sToFind)).Font.Bold = True
            iSeek = InStr(iSeek + 1, rCell.Value, sToFind)
        Loop
    Next i
Next rCell

End Sub

The source for this solution is from: http://www.vbaexpress.com/forum/showthread.php?52245-Make-specific-text-bold-in-cells

Upvotes: 1

ChaPPer5
ChaPPer5

Reputation: 105

Another way for people who need a quick fix and aren't comfortable using VBA:

  1. Copy and paste the cell range into a word document (it will look messy at first).
  2. Go to "replace" (top right).
  3. Type the word you want to make bold into the "Find what:" field and the same into the "Replace with:" field, then when you are on the "Replace With:" box press CTRL B. You should see "Format: Font: Bold" appear beneath it.
  4. Click Replace All and you should see all the targeted words go bold.
  5. Hover over your Excel text (currently in Word) and you should see a small symbol of 4 arrows appear at the top left of the text. Click that to highlight the cells, then you can copy them and paste back into excel.

Not the fastest way but if you're not familiar with VBA and need a quick fix this will work for you!

Not just for Bold: CTRL I for italics, CTRL U for underlined.

Upvotes: 1

anish
anish

Reputation: 61

I would say use this dynamic formula -

Range("A1").Characters(worksheetfunction.find("Excel",Range("A1").value,1),len("Excel")).font.bold = True

Upvotes: 6

Related Questions