Lookup horizontally, that will populate a vertical list based on a cell having any value

nzskater

New Member
Joined
Apr 18, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Would appreciate some help creating a formula to make my life a little easier. I need to create a two column list based on the information below (actual data set is over 1000 rows and 320 reference columns).

As in the example below, I need the output to list each instance of an Item # (C2~) where there is a value in G2~, but the returned value that it populates in the list needs to be from the header in G1~. It needs to ignore any ref # values that have an empty cell, and it needs to output the data vertically. Hopefully that makes sense.

I don't have control of the input sheet or output format as it's system driven. It's a large data set (over 300k possible permutations) hence wanting to do this systematically instead of manually... Any help would be much appreciated.

Output list example is A7-B18.

Screenshot 2024-04-19 at 12.31.17.png
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Cannot manipulate data in a picture. Please re-upload your sample using XL2BB so that we don't have to re-type your data to test a solution.
 
Upvote 0
i create this function:
VBA Code:
Public Function CustomLookup(ByVal indexarray As Range, ByVal loouprange As Range, ByVal Criteria As String)
    Dim cll As Range, fcl As Range, rng As Range
    Dim iarr() As Variant
    Dim icoll As New Collection, lcoll As New Collection
    Dim i As Integer
    Set indexarray = Intersect(indexarray, ActiveSheet.UsedRange)
    Set loouprange = Intersect(loouprange, ActiveSheet.UsedRange)
    If Intersect(indexarray.Resize(indexarray.Rows.Count, Columns.Count), loouprange) Is Nothing Then
        CustomLookup = CVErr(xlErrRef)
        Exit Function
    End If
    For Each cll In indexarray
        If Not IsEmpty(cll) And Not IsError(cll) Then
            Set rng = Intersect(cll.Resize(1, Columns.Count), loouprange)
            If Not rng Is Nothing Then
                For Each fcl In rng
                    If Not Criteria = "" Then
                        If fcl.Value = Criteria Then
                            icoll.Add cll.Value
                            lcoll.Add fcl.Offset(loouprange.Cells(1).Row - fcl.Row)
                        End If
                    Else
                        If Not IsEmpty(fcl) Then
                            icoll.Add cll.Value
                            lcoll.Add fcl.Offset(loouprange.Cells(1).Row - fcl.Row)
                        End If
                    End If
                Next fcl
            End If
        End If
    Next cll
    If icoll.Count > 0 Then
        ReDim iarr(1 To icoll.Count, 1 To 2)
        For i = 1 To icoll.Count
            iarr(i, 1) = icoll(i)
            iarr(i, 2) = lcoll(i)
        Next i
        CustomLookup = iarr
    Else
        CustomLookup = CVErr(xlErrNA)
    End If
End Function

in your data sheet, enter this formula:
Excel Formula:
=CustomLookup(A2:A3,G1:U3,"")
to lookup for all nonblank data or
Excel Formula:
=CustomLookup(A2:A3,G1:U3,"type your lookup value here")
to lookup exact your text you want
 
Upvote 0
Cannot manipulate data in a picture. Please re-upload your sample using XL2BB so that we don't have to re-type your data to test a solution.
Thanks for your input - it looks like policy is preventing me from installing the add-in, otherwise I would have posted using it. Apologies!
 
Upvote 0
i create this function:
VBA Code:
Public Function CustomLookup(ByVal indexarray As Range, ByVal loouprange As Range, ByVal Criteria As String)
    Dim cll As Range, fcl As Range, rng As Range
    Dim iarr() As Variant
    Dim icoll As New Collection, lcoll As New Collection
    Dim i As Integer
    Set indexarray = Intersect(indexarray, ActiveSheet.UsedRange)
    Set loouprange = Intersect(loouprange, ActiveSheet.UsedRange)
    If Intersect(indexarray.Resize(indexarray.Rows.Count, Columns.Count), loouprange) Is Nothing Then
        CustomLookup = CVErr(xlErrRef)
        Exit Function
    End If
    For Each cll In indexarray
        If Not IsEmpty(cll) And Not IsError(cll) Then
            Set rng = Intersect(cll.Resize(1, Columns.Count), loouprange)
            If Not rng Is Nothing Then
                For Each fcl In rng
                    If Not Criteria = "" Then
                        If fcl.Value = Criteria Then
                            icoll.Add cll.Value
                            lcoll.Add fcl.Offset(loouprange.Cells(1).Row - fcl.Row)
                        End If
                    Else
                        If Not IsEmpty(fcl) Then
                            icoll.Add cll.Value
                            lcoll.Add fcl.Offset(loouprange.Cells(1).Row - fcl.Row)
                        End If
                    End If
                Next fcl
            End If
        End If
    Next cll
    If icoll.Count > 0 Then
        ReDim iarr(1 To icoll.Count, 1 To 2)
        For i = 1 To icoll.Count
            iarr(i, 1) = icoll(i)
            iarr(i, 2) = lcoll(i)
        Next i
        CustomLookup = iarr
    Else
        CustomLookup = CVErr(xlErrNA)
    End If
End Function

in your data sheet, enter this formula:
Excel Formula:
=CustomLookup(A2:A3,G1:U3,"")
to lookup for all nonblank data or
Excel Formula:
=CustomLookup(A2:A3,G1:U3,"type your lookup value here")
to lookup exact your text you want
Thanks, that both works and does not - in isolation of the test sheet and against column A it works, but if I change the range in the CustomLookup formula it does not - it seems to error for any range outside of column A. But I got what I needed - just modified the data set to exclude unneeded columns and used A, and it worked. Thank you, you saved someone manually checking 300k lines and produced a list of 32k entries :)
 
Upvote 0
Thanks, that both works and does not - in isolation of the test sheet and against column A it works, but if I change the range in the CustomLookup formula it does not - it seems to error for any range outside of column A. But I got what I needed - just modified the data set to exclude unneeded columns and used A, and it worked. Thank you, you saved someone manually checking 300k lines and produced a list of 32k entries :)
yeah, it worked when indexarray in left of lookuprange, if you want it work for another index array, you can change like this:
VBA Code:
Public Function CustomLookup(ByVal indexarray As Range, ByVal loouprange As Range, ByVal Criteria As String)
    Dim cll As Range, fcl As Range, rng As Range
    Dim iarr() As Variant
    Dim icoll As New Collection, lcoll As New Collection
    Dim i As Long 'if your data too large then change this to long
    Set indexarray = Intersect(indexarray, ActiveSheet.UsedRange)
    Set loouprange = Intersect(loouprange, ActiveSheet.UsedRange)
    If Intersect(indexarray.Resize(indexarray.Rows.Count, Columns.Count), loouprange) Is Nothing Then
        CustomLookup = CVErr(xlErrRef)
        Exit Function
    End If
    For Each cll In indexarray
        If Not IsEmpty(cll) And Not IsError(cll) Then
            Set rng = Intersect(cll.Worksheet.Cells(cll.Row, 1).Resize(1, Columns.Count), loouprange) 'change this
            If Not rng Is Nothing Then
                For Each fcl In rng
                    If Not Criteria = "" Then
                        If fcl.Value = Criteria Then
                            icoll.Add cll.Value
                            lcoll.Add fcl.Offset(loouprange.Cells(1).Row - fcl.Row)
                        End If
                    Else
                        If Not IsEmpty(fcl) Then
                            icoll.Add cll.Value
                            lcoll.Add fcl.Offset(loouprange.Cells(1).Row - fcl.Row)
                        End If
                    End If
                Next fcl
            End If
        End If
    Next cll
    If icoll.Count > 0 Then
        ReDim iarr(1 To icoll.Count, 1 To 2)
        For i = 1 To icoll.Count
            iarr(i, 1) = icoll(i)
            iarr(i, 2) = lcoll(i)
        Next i
        CustomLookup = iarr
    Else
        CustomLookup = CVErr(xlErrNA)
    End If
End Function
 
Upvote 0
A formula solution
Fluff.xlsm
ABCDEFGHIJKLMNOPQ
1ref 1ref 2ref 3ref 4ref 5ref 6ref 7ref 8ref 9ref 10ref 11
2ITM-01aaaa
3ITM-02aaaa
4ITM-03aaaaaa
5
6
7
8
9
10
11
12
13ITM-01ref 1
14ITM-01ref 7
15ITM-01ref 9
16ITM-01ref 11
17ITM-02ref 3
18ITM-02ref 5
19ITM-02ref 6
20ITM-02ref 7
21ITM-03ref 2
22ITM-03ref 5
23ITM-03ref 7
24ITM-03ref 9
25ITM-03ref 10
26ITM-03ref 11
Data
Cell Formulas
RangeFormula
A13:B26A13=HSTACK(TOCOL(IF(G2:Q10<>"",C2:C10,1/0),2),TOCOL(IF(G2:Q10<>"",G1:Q1,1/0),2))
Dynamic array formulas.
 
Upvote 0
Solution
An alternative means with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Column1"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Value"})
in
    #"Removed Columns"
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,969
Messages
6,175,682
Members
452,667
Latest member
vanessavalentino83

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