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.
![](https://static.wixstatic.com/media/af7fa4_915f5bdb7cba4522a9a794daab7fcca8~mv2.png/v1/fill/w_890,h_812,al_c,q_90,enc_auto/af7fa4_915f5bdb7cba4522a9a794daab7fcca8~mv2.png)
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)
![](https://static.wixstatic.com/media/af7fa4_901cfca7ef054362ac2766a3204b572e~mv2.png/v1/fill/w_980,h_593,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/af7fa4_901cfca7ef054362ac2766a3204b572e~mv2.png)
Press the play button in Visual Basic and your text will be boldfaced.
![](https://static.wixstatic.com/media/af7fa4_16994567cde546a28ee66cb417927c01~mv2.png/v1/fill/w_890,h_812,al_c,q_90,enc_auto/af7fa4_16994567cde546a28ee66cb417927c01~mv2.png)
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