excelvba

Excel VBA - Do something if cells in range start with specific string


I'm trying to bold all cells in my column A range if the cell value starts with 5 spaces. Currently this is what I'm trying; producing the error code:

Run-time error '13':
Type mismatch

    Sub Bold ()
    Set BoldRange = Sheets("Formatted Data").Range("A13:A100000")
        
        If BoldRange.Value Like "     *" Then
           BoldRange.Font.Bold = True
        End If
    End Sub

Note, the error is occurring on line If BoldRange.Value Like " *" Then. Not too sure of what I'm doing wrong?


Solution

  • As suggested above, I've modified to use a For...Next loop.

    Sub Bold ()
    Dim rcell As Range, BoldRange As Range
    Set BoldRange = Sheets("Formatted Data").Range("A13:A100000")
    
    For Each rcell In BoldRange.Cells
        If rcell.Value Like "     *" Then rcell.Font.Bold = True
    Next rcell
    End Sub