简体   繁体   中英

Excel VBA: How to change font color of text in parenthesis?

I have a code that i copied from another forum that runs through a range of cells and copies the characters in the cells that contain parenthesis to an adjacent cell. What i would like to do is simply change the font color of the characters inside the parenthesis along with the parenthesis. I have managed to tweak the code so that i include the parenthesis but i dont know how to change the font color. I believe it requires one line of code maybe two if im not mistaken. Here is the code:

Dim n, i As Long
With CreateObject("vbscript.regexp")
.Pattern = "(\(\w+\))"
.Global = True
    For i = 1 To 10
        Set myMatches = .Execute(Cells(i, 1))
    
        For Each n In myMatches
            Cells(i, 2).Value = Mid(n, 1, Len(n) - 0)
        Next n
    Next i
End With

As you can see the ' Cells(i, 2).Value = Mid(n, 1, Len(n) - 0)' is what needs to be changed.

Thank you.

' The code below will change chars 1 to 4 in the cell red

.Cells.Characters(1, 4).Font.Color = vbRed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM