Forum Discussion
jurkasovak
Jan 27, 2023Copper Contributor
xlookup with format
Hi! How I can use function xlookup with format. For example If I want find cell with color... Thanks.
- Jan 30, 2023
Here is a simplistic function.
Function XLookupColor(ColorCell As Range, LookupRange As Range, _ ResultRange As Range, IfNotFound As Variant) As Variant Dim LookupColor As Long Dim i As Long Application.Volatile LookupColor = ColorCell.Interior.Color For i = 1 To LookupRange.Count If LookupRange(i).Interior.Color = LookupColor Then XLookupColor = ResultRange(i).Value Exit Function End If Next i XLookupColor = IfNotFound End Function
The function has four arguments:
- ColorCell: a cell with the fill color to search for.
- LookupRange: the range with colored cells.
- ResultRange: the range with values to return. This must be the same size as the lookup range.
- IfNotFound: the value to return if the color is not found. Use "" if you want to return a blank.
Warning: the result will not be updated automatically if you change the color of the color cell or the color range. Press F9 to force a recalculation.
Sample workbook attached. You'll have to allow macros when you open it.
HansVogelaar
Jan 27, 2023MVP
Excel's lookup functions can only search for a cell's value, not its formatting.
If the lookup range has been colored using conditional formatting, you should use the conditions of the conditional formatting rules, Otherwise, you'd need VBA code.
jurkasovak
Jan 30, 2023Copper Contributor
Hi Hans!
Thank you for answer. Unfortunately, I cannot use conditional formatting, because the source file does not have data that is colored according to the rule (my colleague marks the status of the order according to her own color).
Do you have VBA code?
Thanks, Katka.
Thank you for answer. Unfortunately, I cannot use conditional formatting, because the source file does not have data that is colored according to the rule (my colleague marks the status of the order according to her own color).
Do you have VBA code?
Thanks, Katka.
- HansVogelaarJan 30, 2023MVP
Here is a simplistic function.
Function XLookupColor(ColorCell As Range, LookupRange As Range, _ ResultRange As Range, IfNotFound As Variant) As Variant Dim LookupColor As Long Dim i As Long Application.Volatile LookupColor = ColorCell.Interior.Color For i = 1 To LookupRange.Count If LookupRange(i).Interior.Color = LookupColor Then XLookupColor = ResultRange(i).Value Exit Function End If Next i XLookupColor = IfNotFound End Function
The function has four arguments:
- ColorCell: a cell with the fill color to search for.
- LookupRange: the range with colored cells.
- ResultRange: the range with values to return. This must be the same size as the lookup range.
- IfNotFound: the value to return if the color is not found. Use "" if you want to return a blank.
Warning: the result will not be updated automatically if you change the color of the color cell or the color range. Press F9 to force a recalculation.
Sample workbook attached. You'll have to allow macros when you open it.
- jurkasovakFeb 01, 2023Copper Contributor
- HansVogelaarFeb 01, 2023MVP
I don't see how that would work. What exactly do we have to look up and where?
- PeterBartholomew1Jan 30, 2023Silver Contributor
Your VBA programming is better than mine. I have been so locked into Lambda that I have largely forgotten VBA! My attempt was
Sub WriteStatus() Dim rng As Range, c As Range Dim state As String Dim color Set rng = Range("coloredRange") For Each c In rng color = c.Interior.ColorIndex Select Case color Case 43 state = "paid" Case 44 state = "urgent" Case 3 state = "overdue" Case 6 state = "due" Case 33 state = "in process" Case Else state = "not found" End Select c.Value = state Next End Sub