How do I get Excel to return the column header of text that appears in an array?

PCnetMD

New Member
Joined
Oct 14, 2018
Messages
1
I need to determine which column a specific item is located.

1234
waterlettucechickenice cream
colatomatobeefcake
juicecucumberporkpie
coffeeokralambbrownie
teacarrotshrimpcookie
milkonionfishcandy

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>

How do I get Excel to return the column header of text that appears in the above array? Everything I have found only works for number(values).

Thank you,
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I need to determine which column a specific item is located.

1234
waterlettucechickenice cream
colatomatobeefcake
juicecucumberporkpie
coffeeokralambbrownie
teacarrotshrimpcookie
milkonionfishcandy

<tbody>
</tbody>

How do I get Excel to return the column header of text that appears in the above array? Everything I have found only works for number(values).

Thank you,

=INDEX(A1:D1,MAX((A2:D7="pork")*COLUMN(A:D)))
Ctrl shift enter for the array formula.
 
Upvote 0
Assume your data is arranged as follows:

Data Range
A
B
C
D
E
F
G
H
I
1
water​
1​
2​
3​
4​
2
cola​
water​
lettuce​
chicken​
ice cream​
3
juice​
cola​
tomato​
beef​
cake​
4
coffee​
juice​
cucumber​
pork​
pie​
5
tea​
coffee​
okra​
lamb​
brownie​
6
milk​
tea​
carrot​
shrimp​
cookie​
7
lettuce​
milk​
onion​
fish​
candy​
8
tomato​
9
cucumber​
10
okra​
11
carrot​
12
onion​
13
chicken​
14
beef​
15
pork​
16
lamb​
17
shrimp​
18
fish​
19
ice cream​
20
cake​
21
pie​
22
brownie​
23
cookie​
24
candy​

<tbody>
</tbody>

then the following VBA will do what you need

Code:
Option Explicit


Sub foo()
    Dim i As Long, c As Range
    Dim rng As Range, x As Long
    Set rng = Range("F2:I7")
    Dim lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
        For Each c In rng
            If Range("A" & i) = c Then
                x = c.Column
                Range("B" & i) = Cells(1, x)
            End If
        Next c
    Next i
End Sub
 
Last edited:
Upvote 0
Use this formula

=SUMPRODUCT((a1:d6="tea")*(COLUMN(a1:d6)))

you will get answer as 1
 
Upvote 0
Does a value like okra repeat? If it does, do you need all of the corresponding headers? By the way, is the set of 1, 2, 3, and 4 the header set?
 
Upvote 0

Forum statistics

Threads
1,214,625
Messages
6,120,598
Members
448,973
Latest member
ksonnia

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