Share via

Excel VBA - Return Colum header value

Anonymous
2024-03-12T08:24:56+00:00

Hello All,

i have a table in which macro searches for cells that are conditionally formatted RED . Looking for a piece of code which will return the first column header of cell if cell is formatted RED.

*red indicates the cell color is conditionally formatted RED

the macro searches for first RED cell in range C2-H2 and stops when it encounters first red cell then iterates to C3-H3

i am looking for an output like the below table. the code should return the first red cell only in a row

Plant Part Number Red On
a Part1 Past Due
a Part2 1-1-2024

and the list goes on

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2024-03-13T07:09:51+00:00

Sub Test()
Dim Where As Range, WhereRow As Range, R As Range
Dim C As New Collection
Dim Data, Item
Dim i As Long, j As Long

'Parse the cells
Set Where = Range("A1").CurrentRegion
C.Add Array(Range("A1"), Range("B1"), "Red On")
For Each WhereRow In Where.Rows
For Each R In WhereRow.Cells
If R.DisplayFormat.Interior.Color = vbRed Then
C.Add Array(Range("A" & R.Row), Range("B" & R.Row), Cells(1, R.Column))
Exit For
End If
Next
Next
If C.Count = 0 Then
MsgBox "No Items found"
Exit Sub
End If
'Compile the output
ReDim Data(1 To C.Count, 1 To 3)
For Each Item In C
i = i + 1
For j = 1 To 3
Data(i, j) = Item(j - 1)
Next
Next
'Flush into a new sheet
Worksheets.Add
Range("A1").Resize(UBound(Data), UBound(Data, 2)).Value = Data
End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-03-13T05:26:50+00:00

    Hi @Andreas Killer. many thanks for your code it worked but i missed one piece in my initial post . I have re-edited it. Currently its returning all the red cells in the range . i wanted only fist red cell . can you please modify

    Was this answer helpful?

    0 comments No comments
  2. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2024-03-12T09:37:47+00:00

    Sub Test()
    Dim Where As Range, R As Range
    Dim C As New Collection
    Dim Data, Item
    Dim i As Long, j As Long

    'Parse the cells
    Set Where = Range("A1").CurrentRegion
    C.Add Array(Range("A1"), Range("B1"), "Red On")
    For Each R In Where
    If R.DisplayFormat.Interior.Color = vbRed Then
    C.Add Array(Range("A" & R.Row), Range("B" & R.Row), Cells(1, R.Column))
    End If
    Next
    If C.Count = 0 Then
    MsgBox "No Items found"
    Exit Sub
    End If
    'Compile the output
    ReDim Data(1 To C.Count, 1 To 3)
    For Each Item In C
    i = i + 1
    For j = 1 To 3
    Data(i, j) = Item(j - 1)
    Next
    Next
    'Flush into a new sheet
    Worksheets.Add
    Range("A1").Resize(UBound(Data), UBound(Data, 2)).Value = Data
    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-03-12T08:52:04+00:00

    Sir i do not have option to attach the file. Disabled by my organization. it basically looks something like this.

    Current code checks red cell in row range C2-H2 .i want something that will return header of the cell where it found red and return table like this in new sheet

    Plant Part Number Red On
    a Part1 Past due
    a Part2 1-14-2024

    Was this answer helpful?

    0 comments No comments
  4. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2024-03-12T08:34:35+00:00

    Was this answer helpful?

    0 comments No comments