Macro to Bold tet within wrapped text in B2

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have code below to bold this portion of text "Do NOT amend my links." in B2 that contains other text as well that is wrapped

It would be appreciated if someone coild amend my code

Code:
Sub BoldTextInCell()
    Dim searchText As String
    Dim cellText As String
    Dim cellRange As Range
    Dim startPos As Long
    Dim found As Boolean

    ' Set the text you want to bold
    searchText = "Do NOT amend my links"

    ' Set the cell range
    Set cellRange = ThisWorkbook.Sheets("Email").Range("B2")

    ' Get the cell's text
    cellText = cellRange.Value

    ' Initialize the starting position
    startPos = 1
    found = False

    Do
        startPos = InStr(startPos, cellText, searchText, vbTextCompare)
        If startPos > 0 Then
            cellRange.Characters(startPos, Len(searchText)).Font.Bold = True
            startPos = startPos + 1
            found = True
        End If
    Loop While startPos > 0

    ' Display a message if the text was not found
    If Not found Then
        MsgBox "Text not found in the cell."
    End If
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi,

This should work:
VBA Code:
Sub BoldTextInCell()
    Dim searchText As Variant, str As Variant
    Dim cellText As String
    Dim cellRange As Range
    Dim startPos As Long
    Dim found As Boolean

    ' Set the cell range
    Set cellRange = ThisWorkbook.Sheets("Email").Range("B2")

    ' Get the cell's text
    cellText = cellRange.Value

    ' Initialize the starting position
    startPos = 1
    found = False
 
    ' Set the text you want to bold
    searchText = Array("Do NOT amend my links", "Your other text", "You can add more")
    For Each str In searchText
      Do
          startPos = InStr(startPos, cellText, str, vbTextCompare)
          If startPos > 0 Then
              cellRange.Characters(startPos, Len(str)).Font.Bold = True
              startPos = startPos + 1
              found = True
          End If
      Loop While startPos > 0
    Next
    ' Display a message if the text was not found
    If Not found Then
        MsgBox "Text not found in the cell."
    End If
End Sub
 
Last edited by a moderator:
Upvote 0
Many Thanks for your code

I get a run time error with this portion of the code

Code:
  startPos = InStr(startPos, cellText, str, vbTextCompare)
 
Upvote 0
Ok, try to exit loop after str found. Also it will be faster I guess.
VBA Code:
Sub BoldTextInCell()
    Dim searchText As Variant, str As Variant
    Dim cellText As String
    Dim cellRange As Range
    Dim startPos As Long
    Dim found As Boolean

    ' Set the cell range
    Set cellRange = ThisWorkbook.Sheets("Email").Range("B2")

    ' Get the cell's text
    cellText = cellRange.Value

    ' Initialize the starting position
    startPos = 1
    found = False
 
    ' Set the text you want to bold
    searchText = Array("Do NOT amend my links", "Your other text", "You can add more")
    For Each str In searchText
      Do
          startPos = InStr(startPos, cellText, str, vbTextCompare)
          If startPos > 0 Then
              cellRange.Characters(startPos, Len(str)).Font.Bold = True
              startPos = startPos + 1
              found = True
              Exit Do
          End If
      Loop
    Next
    ' Display a message if the text was not found
    If Not found Then
        MsgBox "Text not found in the cell."
    End If
End Sub
Also you won't need the while condition.
 
Last edited by a moderator:
Upvote 0
Tested:
VBA Code:
Sub BoldTextInCell()
    Dim searchText As Variant, str As Variant
    Dim cellText As String
    Dim cellRange As Range
    Dim startPos As Long
    Dim found As Boolean

    ' Set the cell range
    Set cellRange = ThisWorkbook.Sheets("Email").Range("B2")

    ' Get the cell's text
    cellText = cellRange.Value

    ' Initialize the starting position
    found = False
 
    ' Set the text you want to bold
    searchText = Array("Do NOT amend my links", "finalise", "check")
    For Each str In searchText
      startPos = 1
      Do
          startPos = InStr(startPos, cellText, str, vbTextCompare)
          If startPos > 0 Then
              cellRange.Characters(startPos, Len(str)).Font.Bold = True
              startPos = startPos + 1
              found = True
          End If
      Loop While startPos > 0
    Next
    ' Display a message if the text was not found
    If Not found Then
        MsgBox "Text not found in the cell."
    End If
End Sub
I don't know why but it doesn't work good with the formula.
 
Last edited by a moderator:
Upvote 0
in code:
searchText = Array("Do NOT amend my links", "finalise", "check")
but in cell
"Do NOT amend any of my links"
 
Upvote 0
Looking at the sample file in post #5, the text that you see is the result of a formula, you are not going to be able to bold part of the result from a formula.

I assume you want to keep the formula though. You could move your formula to another sheet and move the value to where you want it. In the below example, your formula would be in cell A1 of the 'Macro' sheet (could be anywhere), the code will bring in the value from that sheet and then make the requested line bold:
VBA Code:
Sub BoldWithFind()
    Dim rCell As Range, rStr As String
   
    Set rCell = Range("B2")
   
    rCell = Sheets("Macro").Range("A1").Value
   
    rStr = "Do NOT amend any of my links."
    rCell.Characters(InStr(rCell, rStr), Len(rStr)).Font.Bold = True
End Sub
 
Upvote 0
Solution
Here is an option where you create your text string in VBA rather than the formula:
VBA Code:
Sub BoldWithFind()
    Dim rCell As Range, rStr As String, dCell As Range
   
    Set rCell = Range("B2") ' target cell
    Set dCell = Range("F1") ' date cell
    rStr = "Do NOT amend any of my links."

    rCell = _
        "Good Morning Guys" & vbNewLine & vbNewLine & _
        "Just a reminder to let me have your schedules by no later than " & Format(Range("F1"), "dd/mm/yyyy.") & vbNewLine & vbNewLine & _
        "Should these be completed, let me have these so I can check and finalise." & vbNewLine & vbNewLine & _
        rStr & vbNewLine & vbNewLine & _
        "Regards" & vbNewLine & vbNewLine & _
        "Howard"

    rCell.Characters(InStr(rCell, rStr), Len(rStr)).Font.Bold = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,127
Messages
6,123,203
Members
449,090
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top