1

I'm pretty knew to VBA and I'm trying to check if in the specific range of column M if there's a negative number and if it is to change the font to red. This is only working for the first number in my report. I have a feeling that I made a small mistake so please let me know.

Sub Format_M_Column()

    Range("M:M").Select
    With Selection
        Selection.NumberFormat = "0.00"
        .Value = .Value
    End With

    For Each cell In Range("M:M")
        If cell.Value < 0 Then
            cell.Font.ColorIndex = 3
            Exit For
        End If
    Next cell

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100
Fizza1
  • 13
  • 2

3 Answers3

1

Remove Exit For. It exits the for - loop:

Excel VBA - exit for loop

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • omg thanks! I was following an answer in this thread and it mentioned to keep it. I'm guessing i wasn't understanding correctly why that specific person had to keep it. – Fizza1 Jul 31 '18 at 14:05
1

as ScottCraner pointed out this should be all you need and should be much faster without that loop.

Sub Format_M_Column()
    With Range("M:M")
        .NumberFormat = "0.00;[Red]-0.00"
        .Value = .Value
    End With
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
0

Try this code (May be faster than yours)

Sub Format_M_Column()
Dim rng As Range
Dim cel As Range

Application.ScreenUpdating = False
    Set rng = Intersect(ActiveSheet.UsedRange, ActiveSheet.Columns(13))

    With rng
        .NumberFormat = "0.00"
        .Value = .Value
        For Each cel In rng
            If cel.Value < 0 Then cel.Font.ColorIndex = 3
        Next cel
    End With
Application.ScreenUpdating = True
End Sub
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95