Home » excel » excel – Highlight cells that contain Chr(10)

excel – Highlight cells that contain Chr(10)

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to highlight all the cells that contain the “Alt + Enter” (aka Chr(10)) character regardless of the other contents of the cell. How can I do this?

How to&Answers:

This should work:

Sub TestForChr()

Dim rng As Range

Set rng = Application.ActiveSheet.UsedRange

For Each cell In rng
    If InStr(1, cell, Chr(10)) Then
        cell.Interior.ColorIndex = 3
    End If
Next

End Sub

If you want to start at specific Row or Column, use this:

Sub AnotherTestForChr()

Dim LastRow As Long, LastColumn As Long
Dim i As Long, y As Long

' LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
' LastColumn = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = Sheets("Sheet1").UsedRange.Rows.Count
LastColumn = Sheets("Sheet1").UsedRange.Columns.Count

For i = 1 To LastRow
    For y = 1 To LastColumn
        If InStr(1, Cells(i, y), Chr(10)) Then
            Cells(i, y).Interior.ColorIndex = 3
        End If
    Next
Next

End Sub

Hope this helps!