Concatenate the header if cells are not blank in a range

Narendra Kumar

New Member
Joined
Nov 25, 2014
Messages
21
Dear Sirs,

Good evening,
Need your help to get desired answer.. with formula like

If cell is non blank in 2nd row(B3:G3) than CONCATENATE the header of non blank cells in range (B3:G3) into H column as result.
Example :
INV/CHG-NAME

<colgroup><col width="106"></colgroup><tbody>
</tbody>
ABC

<colgroup><col width="64"></colgroup><tbody>
</tbody>
DFG

<colgroup><col width="64"></colgroup><tbody>
</tbody>
IHTY

<colgroup><col width="64"></colgroup><tbody>
</tbody>
UUUU

<colgroup><col width="64"></colgroup><tbody>
</tbody>
BBBB

<colgroup><col width="64"></colgroup><tbody>
</tbody>
TOTAL

<colgroup><col width="64"></colgroup><tbody>
</tbody>
RESULT

<colgroup><col width="136"></colgroup><tbody>
</tbody>
ATR1234

<colgroup><col width="106"></colgroup><tbody>
</tbody>
568.00

<colgroup><col></colgroup><tbody>
</tbody>
15.00

<colgroup><col></colgroup><tbody>
</tbody>
155.00

<colgroup><col></colgroup><tbody>
</tbody>
738.00

<colgroup><col></colgroup><tbody>
</tbody>
ABC/IHTY/BBBB

<colgroup><col width="136"></colgroup><tbody>
</tbody>
CRT5678

<colgroup><col width="106"></colgroup><tbody>
</tbody>
1,536.00

<colgroup><col></colgroup><tbody>
</tbody>
852.00

<colgroup><col></colgroup><tbody>
</tbody>
2.00

<colgroup><col></colgroup><tbody>
</tbody>
2,390.00

<colgroup><col></colgroup><tbody>
</tbody>
DFG/UUUU/BBBB

<colgroup><col width="136"></colgroup><tbody>
</tbody>
FHD2341

<colgroup><col width="106"></colgroup><tbody>
</tbody>
852.00

<colgroup><col></colgroup><tbody>
</tbody>
862.00

<colgroup><col></colgroup><tbody>
</tbody>
850.00

<colgroup><col></colgroup><tbody>
</tbody>
2,564.00

<colgroup><col></colgroup><tbody>
</tbody>
ABC/IHTY/UUUU/

<colgroup><col width="136"></colgroup><tbody>
</tbody>
JKD6785

<colgroup><col width="106"></colgroup><tbody>
</tbody>
86.00

<colgroup><col></colgroup><tbody>
</tbody>
952.00

<colgroup><col></colgroup><tbody>
</tbody>
758.00

<colgroup><col></colgroup><tbody>
</tbody>
1,796.00

<colgroup><col></colgroup><tbody>
</tbody>
ABC/IHTY/BBBB/

<colgroup><col width="136"></colgroup><tbody>
</tbody>
MNB5876

<colgroup><col width="106"></colgroup><tbody>
</tbody>
85.00

<colgroup><col></colgroup><tbody>
</tbody>
85.00

<colgroup><col></colgroup><tbody>
</tbody>
12.00

<colgroup><col></colgroup><tbody>
</tbody>
852.00

<colgroup><col></colgroup><tbody>
</tbody>
1,034.00

<colgroup><col></colgroup><tbody>
</tbody>
ABC/DFG/IHTY/BBBB

<colgroup><col width="136"></colgroup><tbody>
</tbody>

<tbody>
</tbody>
Thanks in advance for your help.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
=left(if(trim(c3)<>"",$c$2&"/", "") & if(trim(d3)<>"",$d$2 & "/", "") & if(trim(e3)<>"",$e$2 & "/","") & if(trim(f3)<>"",$f$2 & "/","") & if(trim(g3)<>"",$g$2 & "/",""), len(if(trim(c3)<>"",$c$2&"/", "") & if(trim(d3)<>"",$d$2 & "/", "") & if(trim(e3)<>"",$e$2 & "/","") & if(trim(f3)<>"",$f$2 & "/","") & if(trim(g3)<>"",$g$2 & "/",""))-1)
 
Upvote 0
Mr iggydarsa,

Thank you very much for your quick response.
its working perfectly, but i have the data in 52 column and 1800 rows.
looking for another solution with great expectations.
 
Upvote 0
Rows are not an issue but you cant really do 52 nested IFs.
If you are interested it can be done via macro tho.
 
Upvote 0
its working perfectly, but i have the data in 52 column and 1800 rows.
looking for another solution with great expectations.

Please Note
-------------------
For future questions you may ask, please do not simplify your question for us... doing so will almost always lead to you coming back for help when the solution we give you for the simplified question cannot be applied to your actual data and its layout. One thing you must keep in mind when you ask a question in a forum... the people you are asking to help you know absolutely nothing about your data, absolutely nothing about how it is laid out in the workbook, absolutely nothing about what you want done with it and absolutely nothing about how whatever it is you want done is to be presented back to you as a result... you must be very specific about describing each of these areas, in detail, and you should not assume that we will be able to "figure it out" on our own. Remember, you are asking us for help... so help us to be able to help you by providing the information we need to do so, even if that information seems "obvious" to you (remember, it is only obvious to you because of your familiarity with your the data, its layout and the overall objective for it).
 
Upvote 0
Good morning Mr iggydarsa,
yes, please if thru macro no issue.
thanks
You could try this user-defined function. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. If using Excel 2007 or later your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

Code:
Function Headings(rHdrs As Range, rData As Range) As String
  Dim aHdrs As Variant, aData As Variant
  Dim i As Long
  
  aHdrs = rHdrs.Value
  aData = rData.Value
  For i = 1 To UBound(aHdrs, 2)
    If Len(aData(1, i)) Then Headings = Headings & "/" & aHdrs(1, i)
  Next i
  Headings = Mid(Headings, 2)
End Function


Excel Workbook
ABCDEFGH
1INV/CHG-NAMEABCDFGIHTYUUUUBBBBTOTALRESULT
2ATR123456815155738ABC/IHTY/BBBB
3CRT56781,536.0085222,390.00DFG/UUUU/BBBB
4FHD23418528628502,564.00ABC/IHTY/UUUU
5JKD6785869527581,796.00ABC/IHTY/BBBB
6MNB58768585128521,034.00ABC/DFG/IHTY/BBBB
Sheet1
 
Last edited:
Upvote 0
Peter gave you a UDF to use. If you don't need your result column to be "live" (that is, immediately reactive to changes in the data itself), then you can save placing 1800 formulas in your worksheet and use this macro instead...
Code:
[B]Sub ConcatenateHeadersForDataCells()
  Dim R As Long, X As Long
  Dim Headers As Variant, Data As Variant, Result As Variant
  Const LastDataColumn As String = "BA"
  Const FirstResultCell As String = "BC2"
  Headers = Range("B1:" & LastDataColumn & "1")
  Data = Range("B2:" & LastDataColumn & Cells(Rows.Count, "A").End(xlUp).Row)
  ReDim Result(1 To UBound(Data), 1 To 1)
  For R = 1 To UBound(Data, 1)
    For X = 1 To UBound(Data, 2)
      If Len(Data(R, X)) Then Result(R, 1) = Result(R, 1) & "/" & Headers(1, X)
    Next
    Result(R, 1) = Mid(Result(R, 1), 2)
  Next
  Range(FirstResultCell).Resize(UBound(Result)) = Result
End Sub[/B]
Note: I was not 100% sure of where your data ended or where your result column was located at, so I provided two constants (the Const statements at the beginning of the code) where you can set them (change my guesses if they are wrong).
 
Upvote 0
Code:
Function Headings(rHdrs As Range, rData As Range) As String
  Dim aHdrs As Variant, aData As Variant
  Dim i As Long
  
  aHdrs = rHdrs.Value
  aData = rData.Value
  For i = 1 To UBound(aHdrs, 2)
    If Len(aData(1, i)) Then Headings = Headings & "/" & aHdrs(1, i)
  Next i
  Headings = Mid(Headings, 2)
End Function
@Peter,

You had to know I could not resist this (especially when it's a weekend and I'm bored), right? :devilish:
Code:
[table="width: 500"]
[tr]
	[td]Function Headings(rHdrs As Range, rData As Range) As String
  Headings = Replace(Replace(Application.Trim(Join(Evaluate("IF(LEN(" & rData.Address & "),substitute(" & rHdrs.Address & ","" "",CHAR(1)),"""")"), " ")), " ", "/"), Chr(1), " ")
End Function
[/td]
[/tr]
[/table]



@Narendra Kumar,

If you choose to go with a UDF instead of the macro I posted in Message #8, use the code Peter posted in Message #7 and not the code above.
 
Last edited:
Upvote 0
Mr Rick Rothstein,

Till yesterday night i don't have any idea how to deal with it but, Today i have two perfect solutions from You and Mr Peter SSs.
thank you very much for your help.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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