VBA Function to return a value or Multiple values

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,174
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good afternoon,
I have data in A1:A5 that looks like this (just a sample). In B1 let’s say I would house 101 and in C1 I would like to return Red. One caveat is that if I lookup 102 then I would like to return Green, Blue. Thanks in advance!
Code:
Red:(101,103,105)
Green:(102, 104,105)
Black:(104,111,115,117,119)
Blue:(102,162,169)
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Add this function in a standard module
Code:
Function GetColours(LookFor As String, rng As Range)
Dim cel As Range, txt As String, clr As String, a As Variant, m As String
For Each cel In rng
    txt = cel.Value
    If (txt) <> vbNullString Then
        clr = Left(txt, InStr(txt, ":") - 1)
        txt = Right(txt, Len(txt) - InStr(txt, "("))
        txt = Left(txt, InStr(txt, ")") - 1)
    
        For Each a In Split(txt, ",")
            If LookFor = Trim(a) Then
                If m = "" Then m = clr Else m = m & ", " & clr
                Exit For
            End If
        Next
    End If
Next cel
    GetColours = m
End Function

Put this formula in cell C1
=getcolours(B1,A2:A5)
 
Upvote 0
Thank you so much Yongle! That works perfectly! Thank you again for helping me out with this! I appreciate it!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top