Combine cells value with the header and ignore the blanks

sahil86

New Member
Joined
Nov 20, 2020
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
Hi All

Please if someone can help to achieve the desired result from complex Excel data. I need to combine the cell value with the header and ignore the blank cells.

Please advise if it is possible. Thank you for your time.

NumberRESULT REQUIREDFirst NameSecond NameTelEmail
1​
First Name: ABC - Tel: 123456789ABC
123456789​
2​
Tel: 123456789
123456789​
3​
Second Name: ABD - Email: @@@ABC
123456789​
@@@
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Well let's get the ball rolling. This program delivers 99% of what you want. The only thing missing is that - between Results. Anyway what usually happens now is the A Students will weigh in and do the same with with 3 lines of code. This should at least get the discussion goiong.

VBA Code:
Sub Join1()

Dim Row1 As Long
Dim LastRow As Long
Dim LStr As String
Dim Str1 As String
Dim Str2 As String
Dim Str3 As String
Dim Str4 As String

LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For Row1 = 2 To LastRow

If Cells(Row1, 3) <> "" Then
Str1 = "First Name: " & Cells(Row1, 3).Value & "  "
Else
Str1 = ""
End If

If Cells(Row1, 4) <> "" Then
Str2 = "Second Name: " & Cells(Row1, 4).Value & "  "
Else
Str2 = ""
End If

If Cells(Row1, 5) <> "" Then
Str3 = "Tel: " & Cells(Row1, 5).Value & "  "
Else
Str3 = ""
End If
If Cells(Row1, 6) <> "" Then
Str4 = "Email: " & Cells(Row1, 6).Value & "  "
Else
Str4 = ""
End If

Range("B" & Row1).Value = Str1 & Str2 & Str3 & Str4
Next Row1

End Sub

20-11-20.xlsm
ABCDEF
1NumberRESULT REQUIREDFirst NameSecond NameTelEmail
21First Name: ABC Tel: 123456789ABC123456789
32Tel: 123456789123456789
43Second Name: ABD Tel: 123456789 Email: @@@ABD123456789@@@
Join
 
Upvote 0
You can also try this, if formula suits you

Book1
CDEFGH
4NumberRESULT REQUIREDFirst NameSecond NameTelEmail
51First Name: ABC Tel: 123456789ABC 123456789 
62Tel: 123456789  123456789 
73Second Name: ABD Tel: 123456789 Email: @@@ ABD123456789@@@
Sheet1
Cell Formulas
RangeFormula
E5:H7E5=IFERROR(MID(MID($D5,SEARCH(E$4,$D5)+LEN(E$4)+2,100),1,IFERROR(SEARCH(" ",MID($D5,SEARCH(E$4,$D5)+LEN(E$4)+2,100)),100)),"")
 
Upvote 0
Another option as a UDF
VBA Code:
Function sahil(Rng As Range, HdrRw As Long) As String
   Dim Cl As Range
  
   For Each Cl In Rng
      If Cl <> "" Then sahil = sahil & " - " & Cells(HdrRw, Cl.Column) & ": " & Cl.Value
   Next Cl
   sahil = Mid(sahil, 4)
End Function
Used like
+Fluff v2.xlsm
ABCDEF
1NumberRESULT REQUIREDFirst NameSecond NameTelEmail
21First Name: ABC - Tel: 123456789ABC123456789
32Tel: 123456789123456789
43Second Name: ABC - Tel: 123456789 - Email: @@@ABC123456789@@@
5
Master
Cell Formulas
RangeFormula
B2:B4B2=sahil(C2:F2,1)


Where the 2nd argument is the row number for the headers.

@CA_Punit
I think the OP wants it the other way round.
 
Upvote 0
Fluff Your UserFunction works great. I am going to add it to my library.
 
Upvote 0
Welcome to the MrExcel board!

A couple more options.
- Rows 2-6 using standard worksheet functions available in your Excel 2013
- Rows 9-13 using the following user-defined function. Post back if you are interested in using a udf and need help with how to implement those.

VBA Code:
Function Combine(rData As Range, rHdrs As Range) As String
  Combine = RTrim(Join(Filter(Split(Join(Evaluate(rHdrs.Address & "&"": ""&" & rData.Address), " |") & " ", "|"), ":  ", False), "- "))
End Function

sahil86.xlsm
ABCDEF
1NumberRESULT REQUIREDFirst NameSecond NameTelEmail
21First Name: ABC - Tel: 123456789ABC123456789
32Tel: 123456789123456789
43Second Name: ABC - Tel: 123456789 - Email: @@@ABC123456789@@@
54First Name: ABC - Email: email1ABCemail1
65First Name: Tom - Second Name: Hill - Tel: 56-789 - Email: email2TomHill56-789email2
7
8NumberRESULT REQUIREDFirst NameSecond NameTelEmail
91First Name: ABC - Tel: 123456789ABC123456789
102Tel: 123456789123456789
113Second Name: ABC - Tel: 123456789 - Email: @@@ABC123456789@@@
124First Name: ABC - Email: email1ABCemail1
135First Name: Tom - Second Name: Hill - Tel: 56-789 - Email: email2TomHill56-789email2
Sheet1
Cell Formulas
RangeFormula
B2:B6B2=MID(IF(C2="",""," - "&C$1&": "&C2)&IF(D2="",""," - "&D$1&": "&D2)&IF(E2="",""," - "&E$1&": "&E2)&IF(F2="",""," - "&F$1&": "&F2),4,999)
B9:B13B9=Combine(C9:F9,C$8:F$8)
 
Upvote 0
For those who might be interested, here is Fluff's UDF as a one-liner...
VBA Code:
Function sahil(Rng As Range, HR As Long) As String
  sahil = Mid(Join(Evaluate(Replace(Replace("IF(@="""","""","" - ""&#&"": ""&@)", "@", Rng.Address), "#", Intersect(Rows(HR), Rng.EntireColumn).Address)), ""), 4)
End Function
 
Upvote 0
For those who might be interested, here is Fluff's UDF as a one-liner..
That one-liner could be reduced (ie characters) a tiny bit further ..
VBA Code:
Function sahil(Rng As Range, HR As Long) As String
  sahil = Mid(Join(Evaluate(Replace(Replace("IF(@="""","""","" - ""&#&"": ""&@)", "@", Rng.Address), "#", Rng.Offset(HR - Rng.Row).Address)), ""), 4)
End Function

Just a further comment that if any form of this udf is used, I would recommend using like below so that if any rows are inserted/deleted above the header row the udf would automatically adjust that header row argument on recalculation.
Excel Formula:
=sahil(C2:F2,ROW(C$1))
 
Upvote 0
THANK YOU EVERYONE YOU GUYZ ARE AWESOME. @ Fluffs UDF WORKS GREAT AND ALREADY APPRECIATED BY EASYGUY4U & OTHERS.


God Bless you everyone ?????
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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