use vba change cell color in excel code example

Example 1: excel vba color text in cell

' Occurences of pWord inside pRange's text become bold and red. 
' Case insensitive.
Sub WordToBold(ByRef pRange As Range, ByVal pWord As String, _
    Optional ByVal pPos As Integer = 1)
    Dim pos As Integer
    pos = InStr(pPos, pRange.Value, pWord, vbTextCompare)
    If pos > 0 Then
        With pRange.Characters(pos, Len(pWord))
            .Font.Bold = True
            .Font.Color = vbRed
        End With
        WordToBold pRange, pWord, pos + 1
    End If
End Sub
'--------------------------------------------------------------------------------
Sub TestIt()
    Range("A1") = "Run, Forrest run"
    WordToBold Range("A1"), "run"
End Sub

Example 2: excel vba change format of cell

'VBA code to change the Number Formatting of a cell range:

[A1].NumberFormat = "@"

'There are hundreds of options for Number Formats. The above sets
'cell A1 to display as Text.

Here are a few other Number Formats:

[A1].NumberFormat = "General"
[A1].NumberFormat = "$#,##0.00"
[A1].NumberFormat = "mm-dd-yyyy"

Tags:

Vb Example