Retrieving Column Header values in a grid

mattyblueice

Board Regular
Joined
Jul 24, 2014
Messages
85
Office Version
  1. 365
Platform
  1. MacOS
I have a large client product ID grid I am working with where I am trying to develop a formula that will pick up the column header values (product ID) if an X is present in the cell. The formula is in the Codes column, below is a sample of what I am working with but there are many product codes and clients.

I have researching formulas IF, MATCH and INDEX, but I am stuck about how to get this to work? Can anyone offer any suggestions please?

ABCDEFGH
1ClientCodesB12B14B36B45B68
2SmithB12, B36, B45XXX
3JonesB14, B36, B45XXX
4PaulB12, B14, B36, B45, B68XXXXX

<tbody>
</tbody>
 
First, add the following code as a module to your workbook using Alt+F11...

Function aconcat(a As Variant, Optional sep As String = "") As String
' Harlan Grove, Mar 2002
Dim y As Variant
If TypeOf a Is Range Then
For Each y In a.Cells
aconcat = aconcat & y.Value & sep
Next y
ElseIf IsArray(a) Then
For Each y In a
aconcat = aconcat & y & sep
Next y
Else
aconcat = aconcat & a & sep
End If
aconcat = Left(aconcat, Len(aconcat) - Len(sep))
End Function

Now in C2 control+shift+enter, not just enter:

=REPLACE(aconcat(IF($C2:$G2="X",", "&$C$1:$G$1,"")),1,2,"")
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Assuming the layout you showed in Message #1 (but assuming many more columns and rows of data), here is a UDF (user defined function), which takes no arguments, that you can use...
Code:
[table="width: 500"]
[tr]
	[td]Function Codes() As String
  Dim C As Long, LastCol As String, CallerCell As Range
  [B][COLOR="#FF0000"]Application.Volatile[/COLOR][/B]
  LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
  Set CallerCell = Application.Caller
  For C = 3 To LastCol
    If Len(Cells(CallerCell.Row, C).Value) Then Codes = Codes & ", " & Cells(1, C).Value
  Next
  Codes = Mid(Codes, 3)
End Function[/td]
[/tr]
[/table]
NOTE: I made the function Volatile so that it will react if you add or remove any X's from your grid. If you won't need this functionality, you can remove the red highlighted line of code.


HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use Codes just like it was a built-in Excel function. For example, put this formula in cell B2 and copy it down...

=Codes()

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Last edited:
Upvote 0
Hey Mike, I was putting your formula in, but what is the H2 supposed to be referencing and the J2 below?

Can you use something like this. I first used
=IFERROR(INDEX($B$1:$F$1,SMALL(IF($A$2:$A$4=$H2,IF($B$2:$F$4="x",COLUMN($B$1:$F$1)-COLUMN($B$1)+1)),COLUMNS($J$2:J2)))," ") to find all the codes for the client. Then I used
=CONCATENATE(J2&","&" ",K2&"," &" ",L2) I just did one client but you should get the idea.

ClientB12B14B36B45B68ClientConcat CodesCodes
SmithxxxSmithB12, B36, B45B12B36B45
JonesxxxJonesB14B36B45
PaulxxxxxPaulB12B14B36B45

<tbody>
</tbody>


<tbody>
</tbody>


<tbody>
</tbody>
 
Upvote 0
H2 refers to Smith (at the right). The concatenated formula is in I2 (under Concat Codes). The =iferror(formula is in J2. Copy across.
 
Upvote 0
Thanks. Did you get a result in excel? I set this up exactly as you have if, copied it across and all I get al blank values.
 
Upvote 0
I was able to get the formulas to work as directed. Otherwise, I wouldn't have posted it. Did you just use enter when entering the formulas? Perhaps you should use Cntrl+Shift+Enter instead for these type of formulas. Try that.
 
Upvote 0
Ctrl+shift+enter was the last piece. It worked. Thanks for your help Mike. Have a good thanksgiving!
 
Upvote 0

Forum statistics

Threads
1,215,136
Messages
6,123,247
Members
449,093
Latest member
Vincent Khandagale

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