Finding Header and Row name based on Value

CoachJava

New Member
Joined
Nov 25, 2013
Messages
9
I have a table of 1000 students (names in Column A) and roughly 100 courses (in Row 1) and a list of IDs for the students unique to their respective course. I am given an ID for a student and I need to access the students name and the respective course. For instance, I may receive H19 and I need two formulas to show pull the header (course) which would be "SCI" and the student name (row) would be "Hope". I had heard that INDEX would work with either MATCH or SUBPRODUCT. Any help would be greatly appreciated. Thanks.

A B C D E
1 MAT SCI HIS ENG
2 Bret M45 Y42 214 41Q
3 Dani D43 S75 432 78G
4 Matt Q82 E31 229 98U
5 Lynn L41 E85 127 41R
6 Alex K75 M41 985 41I
7 Amie A41 P21 554 86P
8 Kate J84 Y07 882 22N
9 Sean V25 T31 128 14E
10 Hope O94 H19 014 56L
11 Liam E41 S44 047 13M
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Here is a macro to do it. You will have to change the ranges to suit your needs.

Here is how I setup sheet 1:

Excel 2012
ABCDEFGHI
1MATSCIHISENGLookup ValueStudentCourse
2BretM45Y4221441QH19
3DaniD43S7543278G
4MattQ82E3122998U
5LynnL41E8512741R
6AlexK75M4198541I
7AmieA41P2155486P
8KateJ84Y0788222N
9SeanV25T3112814E
10HopeO94H191456L
11LiamE41S444713M
Sheet1
<p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br />

Sheet after macro runs:

Excel 2012
ABCDEFGHI
1MATSCIHISENGLookup ValueStudentCourse
2BretM45Y4221441QH19HopeSCI
3DaniD43S7543278G
4MattQ82E3122998U
5LynnL41E8512741R
6AlexK75M4198541I
7AmieA41P2155486P
8KateJ84Y0788222N
9SeanV25T3112814E
10HopeO94H191456L
11LiamE41S444713M
Sheet1
<p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br />

macro:
Code:
Sub Find_Header_and_RowName()
    Dim FindString As String
    Dim Rng As Range
    FindString = Sheets("Sheet1").Range("G2").Value ' Change Where Lookup Value Is As Needed
    If Trim(FindString) <> "" Then
        With Sheets("Sheet1").Range("A1:E11") 'Change Range As Needed
            Set Rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
                'Change Ranges Below to Where You Want Values Placed
                Sheets("Sheet1").Range("H2").Value = ActiveCell.End(xlToLeft).Value
                Sheets("Sheet1").Range("I2").Value = ActiveCell.End(xlUp).Value
                Range("G" & Rows.Count).End(xlUp).Offset(1, 0).Select
            Else
                MsgBox "Nothing found"
            End If
        End With
    End If
End Sub
 
Upvote 0
Thanks for the macro. Is there a way to do it without a macro and pull the information formula-driven? I am most concerned about the Column field. Thanks.
 
Upvote 0
since the values are unique maybe something like....

If there is a possibility of duplicates this probably wouldn't work for you

Excel 2012[TABLE="class: grid, width: 500"]
<tbody>[TR="bgcolor: #DAE7F5"]
[TH][/TH]
[TH]A[/TH]
[TH]B[/TH]
[TH]C[/TH]
[TH]D[/TH]
[TH]E[/TH]
[TH]F[/TH]
[TH]G[/TH]
[TH]H[/TH]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: right"][/TD]
[TD]MAT[/TD]
[TD]SCI[/TD]
[TD]HIS[/TD]
[TD]ENG[/TD]
[TD="align: right"][/TD]
[TD]ID[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD]Bret[/TD]
[TD]M45[/TD]
[TD]Y42[/TD]
[TD="align: right"]214[/TD]
[TD]41Q[/TD]
[TD="align: right"][/TD]
[TD]22N[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD]Dani[/TD]
[TD]D43[/TD]
[TD]S75[/TD]
[TD="align: right"]432[/TD]
[TD]78G[/TD]
[TD="align: right"][/TD]
[TD]Name[/TD]
[TD]Subject[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD]Matt[/TD]
[TD]Q82[/TD]
[TD]E31[/TD]
[TD="align: right"]229[/TD]
[TD]98U[/TD]
[TD="align: right"][/TD]
[TD]Kate[/TD]
[TD]ENG[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD]Lynn[/TD]
[TD]L41[/TD]
[TD]E85[/TD]
[TD="align: right"]127[/TD]
[TD]41R[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD]Alex[/TD]
[TD]K75[/TD]
[TD]M41[/TD]
[TD="align: right"]985[/TD]
[TD]41I[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD]Amie[/TD]
[TD]A41[/TD]
[TD]P21[/TD]
[TD="align: right"]554[/TD]
[TD]86P[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD]Kate[/TD]
[TD]J84[/TD]
[TD]Y07[/TD]
[TD="align: right"]882[/TD]
[TD]22N[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]9[/TD]
[TD]Sean[/TD]
[TD]V25[/TD]
[TD]T31[/TD]
[TD="align: right"]128[/TD]
[TD]14E[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]10[/TD]
[TD]Hope[/TD]
[TD]O94[/TD]
[TD]H19[/TD]
[TD="align: right"]14[/TD]
[TD]56L[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]11[/TD]
[TD]Liam[/TD]
[TD]E41[/TD]
[TD]S44[/TD]
[TD="align: right"]47[/TD]
[TD]13M[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
</tbody>[/TABLE]
Sheet1

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Array Formulas[TABLE="width: 100%"]
<tbody>[TR="bgcolor: #DAE7F5"]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]G4[/TH]
[TD="align: left"]{=INDEX(A2:A11,MAX(IF(B2:E11=G2,ROW(A2:A11)-ROW(A2)+1)))}[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]H4[/TH]
[TD="align: left"]{=INDEX(B1:E1,MAX(IF(B2:E11=G2,COLUMN(B1:E1)-COLUMN(B1)+1)))}[/TD]
[/TR]
</tbody>[/TABLE]
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
These are array formulas and must be entered with CTRL-SHIFT-ENTER (command-return on MAC). Change ranges to match your data and there is a space between the " " in the formula.
Excel Workbook
ABCDEFGHI
1MATSCIHISENGLookup ValueStudentCourse
2BretM45Y4221441QH19HopeSCI
3DaniD43S7543278G
4MattQ82E3122998U
5LynnL41E8512741R
6AlexK75M4198541I
7AmieA41P2155486P
8KateJ84Y0788222N
9SeanV25T3112814E
10HopeO94H191456L
11LiamE41S444713M
Sheet
 
Upvote 0
If there is no chance of duplicate

open


Then, formula in H2 will be =IF($G$2<>"",INDEX($A$2:$A$11,SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),ROW($B$2:$E$11)),1)-1),"")

And formula in I2 will be =IF($G$2<>"",INDEX($B$1:$E$1,SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),COLUMN($B$2:$E$11)),1)-1),"")

If there is a chance of duplicate

open


Then formula in H2 will be

=IF($G$2<>"",IF(ISERROR(SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),ROW($B$2:$E$11)),ROWS($H$2:H2))-1),"",INDEX($A$2:$A$11,SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),ROW($B$2:$E$11)),ROWS($H$2:H2))-1)),"")

And formula in I2 will be

=IF($G$2<>"",IF(ISERROR(SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),COLUMN($B$2:$E$11)),ROWS($I$2:I2))-1),"",INDEX($B$1:$E$1,SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),COLUMN($B$2:$E$11)),ROWS($I$2:I2))-1)),"")

And then copy/drag the formula down untill you feel it is safe. By that I mean if you feel that there would be maximum of 5 duplicates then copy the formula till H6 and I6 respectively. I have copied the formula till H11 and I11 assuming the maximum number of duplicates to be 10.
 
Upvote 0
If there is no chance of duplicate

open


Then, formula in H2 will be =IF($G$2<>"",INDEX($A$2:$A$11,SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),ROW($B$2:$E$11)),1)-1),"")

And formula in I2 will be =IF($G$2<>"",INDEX($B$1:$E$1,SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),COLUMN($B$2:$E$11)),1)-1),"")

If there is a chance of duplicate

open


Then formula in H2 will be

=IF($G$2<>"",IF(ISERROR(SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),ROW($B$2:$E$11)),ROWS($H$2:H2))-1),"",INDEX($A$2:$A$11,SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),ROW($B$2:$E$11)),ROWS($H$2:H2))-1)),"")

And formula in I2 will be

=IF($G$2<>"",IF(ISERROR(SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),COLUMN($B$2:$E$11)),ROWS($I$2:I2))-1),"",INDEX($B$1:$E$1,SMALL(IF(ISNUMBER(SEARCH($G$2,$B$2:$E$11,1)),COLUMN($B$2:$E$11)),ROWS($I$2:I2))-1)),"")

And then copy/drag the formula down untill you feel it is safe. By that I mean if you feel that there would be maximum of 5 duplicates then copy the formula till H6 and I6 respectively. I have copied the formula till H11 and I11 assuming the maximum number of duplicates to be 10.

Put this formula and you have to press Ctril+shift+enter as it is an array formula. Just pressing enter won't do the job.
 
Upvote 0
@Nishant94,
In some cases your formula will return the wrong value. In the OP's example if you enter 14 your search formula will find the 14 in 214 and return Bret instead of Hope.
 
Upvote 0
Try this this will work.

If there is no chance of duplicate


Then, formula in H2 will be =INDEX($A$2:$A$11,SMALL(IF($G$2=$B$2:$E$11,ROW($B$2:$E$11)),ROWS($H$2:H2))-1)

And formula in I2 will be =INDEX($B$1:$E$1,SMALL(IF($G$2=$B$2:$E$11,COLUMN($B$2:$E$11)),ROWS($I$2:I2))-1)

If there is a chance of duplicate

Then formula in H2 will be

=IF(ISERROR(SMALL(IF($G$2=$B$2:$E$11,ROW($B$2:$E$11)),ROWS($H$2:H2))-1),"",INDEX($A$2:$A$11,SMALL(IF($G$2=$B$2:$E$11,ROW($B$2:$E$11)),ROWS($H$2:H2))-1))

And formula in I2 will be

=IF(ISERROR(SMALL(IF($G$2=$B$2:$E$11,COLUMN($B$2:$E$11)),ROWS($I$2:I2))-1),"",INDEX($B$1:$E$1,SMALL(IF($G$2=$B$2:$E$11,COLUMN($B$2:$E$11)),ROWS($I$2:I2))-1))

And then copy/drag the formula down untill you feel it is safe. By that I mean if you feel that there would be maximum of 5 duplicates then copy the formula till H6 and I6 respectively. I have copied the formula till H11 and I11 assuming the maximum number of duplicates to be 10.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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