top of page

Excel vba code to use a RegEx search to make part of text inside a cell boldfaced


Stephen R. has posted visual basic code here, which you can use to run a regular expression search on a column in Excel to boldface some but not all of the text inside a cell.

In this example, we have a worksheet on which the Sedona principles have been listed in column A. We want to make the first line in each cell, which lists the principle number, boldfaced.

Press ALT + F11 and in Visual Basic right click on the worksheet in the project list on the left and insert the below code in a new module.

You'll need to update the code so it works on your worksheet and boldfaces the text you're searching for. The Regex search goes on this line:

.Pattern = "Principle [0-9]{1}"

You list the rows to search here:

For RR = 2 To 10

Enter the number of the column you're searching in here [1 = A]:

Set oMatches = .Execute(Cells(RR, 1))

. . . and here:

Cells(RR, 1)

Press the play button in Visual Basic and your text will be boldfaced.

Sub xz()

Dim oMatches As Object, i As Long

Dim RR As Integer

For RR = 2 To 10

With CreateObject("VBScript.RegExp")

.Global = True

.IgnoreCase = True

.Pattern = "Principle [0-9]{1}"

Set oMatches = .Execute(Cells(RR, 1))

For i = 0 To oMatches.Count - 1

Cells(RR, 1).Characters(oMatches(i).FirstIndex + 1, 10).Font.Bold = True

Next i

End With

Next RR

End Sub


bottom of page