How do I use VBA to make a list of all cells rows with red cells? thank you

LAKE, Joe (ISLAND CITY PRACTICE) 25 Reputation points
2024-01-30T15:06:47.1033333+00:00

How do i make excel compile a list of every row in a sheet with a red cell using VBA Thanks

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Excel | For business | Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Tanay Prasad 2,250 Reputation points
    2024-02-01T08:39:04.4333333+00:00

    Hi, Here's the VBA Code-

    Sub ListRowsWithRedCells()
        Dim ws As Worksheet
        Dim rng As Range
        Dim cell As Range
        Dim redRows As Collection
        Dim outputRange As Range
        Dim outputRow As Range
        
        ' Set the worksheet
        Set ws = ThisWorkbook.Sheets("YourSheetName") ' Replace "YourSheetName" with the actual sheet name
        
        ' Set the range to check (adjust as needed)
        Set rng = ws.UsedRange
        
        ' Initialize collection to store rows with red cells
        Set redRows = New Collection
        
        ' Loop through each row in the specified range
        For Each cell In rng.Rows
            ' Check if any cell in the row has a red font or fill color
            If HasRedColor(cell) Then
                ' Add the entire row to the collection
                redRows.Add cell.EntireRow
            End If
        Next cell
        
        ' Create a new worksheet to output the results
        Set outputRange = Worksheets.Add.Range("A1")
        
        ' Loop through the collection and copy rows to the new worksheet
        For Each outputRow In redRows
            outputRow.Copy outputRange
            Set outputRange = outputRange.Offset(1)
        Next outputRow
    End Sub
    
    Function HasRedColor(rng As Range) As Boolean
        ' Check if any cell in the range has a red font or fill color
        Dim cell As Range
        For Each cell In rng.Cells
            If cell.Font.Color = RGB(255, 0, 0) Or cell.Interior.Color = RGB(255, 0, 0) Then
                HasRedColor = True
                Exit Function
            End If
        Next cell
        HasRedColor = False
    End Function
    
    

    The code assumes the red color is represented by RGB(255, 0, 0). If your red color differs, adjust the RGB values accordingly. Regards, Tanay

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.