2 way data table search for data

madmathsman

New Member
Joined
Sep 29, 2023
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hi

Have a 2 way data table, example in pic, and want to create a "search" function where you can enter a row title and it outputs the column title if that title has data in.

Eg in example, if you enter "Mon" in search it would output: Maths, Eng on seperate rows.

Any help or advice welcome

Thanks
 

Attachments

  • Screenshot 2023-09-29 093415.png
    Screenshot 2023-09-29 093415.png
    11.3 KB · Views: 3

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Welcome to the forum,

If you have the TOCOL function then maybe the below:
Book1
BCDEFGHIJK
2MathsEnglishHistoryGeographyMusic
3MonMathsEnglishDay:Subjects:
4TueEnglishFriMaths
5WedGeographyGeography
6ThuEnglishMusic
7FriMathsGeographyMusic
8SatMaths
9SunHistoryMusic
Sheet1
Cell Formulas
RangeFormula
K4:K6K4=TOCOL(FILTER(C3:G9,B3:B9=J4,""),1)
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
J4List=$B$3:$B$9



Here's an option without TOCOL:
Book1
BCDEFGHIJK
2MathsEnglishHistoryGeographyMusic
3MonMathsEnglishDay:Subjects:
4TueEnglishFriMaths
5WedGeographyGeography
6ThuEnglishMusic
7FriMathsGeographyMusic
8SatMaths
9SunHistoryMusic
Sheet1
Cell Formulas
RangeFormula
K4:K6K4=LET(x,XLOOKUP(J4,B3:B9,C3:G9),TRANSPOSE(FILTER(x,x<>"")))
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
J4List=$B$3:$B$9
 
Last edited:
Upvote 0
Hi

Realised I only have Office 2016 and I don't have TOCOl,XLOOKUP or FILTER (unless I'm being stupid), im guessing that makes the solution much harder?
 
Upvote 0
Do the subjects have to be in seperate cells or can they be in the same cell but new row as below:
Math.xlsm
ABCDEF
1MathsEnglishHistoryGeographyMusic
2MonMathsEnglish
3TueEnglish
4WedGeography
5ThuEnglish
6FriMathsGeographyMusic
7SatMaths
8SunHistoryMusic
9
10
11
12
13
14Day:Subjects:
15FriMaths Geography Music
Sheet1
Cell Formulas
RangeFormula
B15B15=GetSub(A2:A8,B1:F1,B2:F8,A15)
Cells with Data Validation
CellAllowCriteria
A15List=$A$2:$A$8
 
Upvote 0
Same cell but new row works.
I can't get the formula to work, or the allowing List for cell A15, but thats exactly the output I want

Its good to know the idea is actually possible
 
Upvote 0
Ohh, I Understand the GetSub is probably a placeholder, oops.
But yeah, that concept works, just how to do it in Excel 2016
 
Upvote 0
Sorry, i probably should have explained that.

So this was done with a custom function in the form of VBA code, the code used is below:
VBA Code:
Function GetSub(x As Range, y As Range, t As Range, sv As String)
    Dim xVar As Variant, yVar As Variant, tVar As Variant
    Dim a As Long, s As Long, r As Long
    Dim oVar() As Variant
 
    xVar = x.Value
    yVar = y.Value
    tVar = t.Value
 
    a = Application.Match(sv, xVar, 0)
 
    For s = 1 To UBound(yVar, 2)
        If tVar(a, s) <> "" Then
            ReDim Preserve oVar(r)
            oVar(r) = tVar(a, s)
            r = r + 1
        End If
    Next s
 
    GetSub = Join(oVar, vbCrLf)
End Function

This has been used on the worksheet as below:
Math.xlsm
ABCDEF
1MathsEnglishHistoryGeographyMusic
2MonMathsEnglish
3TueEnglish
4WedGeography
5ThuEnglish
6FriMathsGeographyMusic
7SatMaths
8SunHistoryMusic
9
10
11
12
13
14Day:Subjects:
15FriMaths Geography Music
Sheet1
Cell Formulas
RangeFormula
B15B15=GetSub(A2:A8,B1:F1,B2:F8,A15)
Cells with Data Validation
CellAllowCriteria
A15List=$A$2:$A$8


Let me know if you need to know where to put the code etc...

EDIT: The result cell will need to have wrap text format set
EDIT 2: This is currently returning the value from the table area and not the heading, i can change it to return the headings if you plan to use say an 'x' in the table to denote that subject.
 
Last edited:
Upvote 1
Solution
This is the code that will return the value in the header instead:
VBA Code:
Function GetSub(x As Range, y As Range, t As Range, sv As String)
    Dim xVar As Variant, yVar As Variant, tVar As Variant
    Dim a As Long, s As Long, r As Long
    Dim oVar() As Variant
    
    xVar = x.Value
    yVar = y.Value
    tVar = t.Value
    
    a = Application.Match(sv, xVar, 0)
    
    For s = 1 To UBound(yVar, 2)
        If tVar(a, s) <> "" Then
            ReDim Preserve oVar(r)
            oVar(r) = yVar(1, s)
            r = r + 1
        End If
    Next s
    
    GetSub = Join(oVar, vbCrLf)
End Function
 
Upvote 0
Wow, I'll try this out and let you know If i get into any problems, thanks very much
 
Upvote 0

Forum statistics

Threads
1,215,089
Messages
6,123,058
Members
449,091
Latest member
ikke

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