Determining the placement of a delimiter when concatenating cells with occasional blank values

mweirath

New Member
Joined
Aug 5, 2016
Messages
28
I have a spreadsheet that I am trying to concatenate 5 columns into 1 column. I have already used some VBA code to change the existing two character codes into string literals. There are some codes that I want to ignore so I left them as two digit characters. I am using a concat formula to merge the cells together. I have a few things I do:

First I check to see if the length of the cell is greater than two, and if so I use the value in that cell.
Second I check the LEN of the two adjacent fields to see if they are greater than 2 and if so I add a . for a delimiter.

This works well unless I have a break in between the cells with values greater than two. I figure I could probably write a massive IF statement to handle this scenario, but I was hoping someone might be able to suggest another approach.

My Code:
=CONCATENATE(IF(LEN(H820)>2,H820,""),IF(AND(LEN(H820)>2,LEN(I820)>2), ".",""),IF(LEN(I820)>2,I820,""),IF(AND(LEN(I820)>2,LEN(J820)>2), ".",""),IF(LEN(J820)>2,J820,""),IF(AND(LEN(J820)>2,LEN(K820)>2), ".",""),IF(LEN(K820)>2,K820,""),IF(AND(LEN(K820)>2,LEN(L820)>2), ".",""),IF(LEN(L820)>2,L820,""))

My problem is with the items in the 5th and 6th row. I am trying to figure out the best way to address that situation. Also the value that shows up in the column could be a variety of things, I have simplified the entry here for demonstrative purposes.

Concat ColumnColumn1Column2Column3
Value1Value1AA
Value1.Value2Value1Value2
Value2AAValue2
Value1Value3Value1Value3
Value1Value3Value1AAValue3

<tbody>
</tbody>
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Maybe something like this? Considering 5 columns will always have values.

=IF(SUM(LEN(D4&E4&F4&G4&H4))>10,RIGHT(IF(LEN(D4)>2,"."&D4,"")&IF(LEN(E4)>2,"."&E4,"")&IF(LEN(F4)>2,"."&F4,"")&IF(LEN(G4)>2,"."&G4,"")&IF(LEN(H4)>2,"."&H4,""),LEN(IF(LEN(D4)>2,"."&D4,"")&IF(LEN(E4)>2,"."&E4,"")&IF(LEN(F4)>2,"."&F4,"")&IF(LEN(G4)>2,"."&G4,"")&IF(LEN(H4)>2,"."&H4,"")) - 1),"")


Val.Val1.VBB.CCCValVal1AAVBBCCC
Val.Val.ValAAAAValValVal
Val.Val.ValAAValAAValVal

<colgroup><col><col span="5"></colgroup><tbody>
</tbody>
 
Upvote 0
If your values have no spaces in them and you want a "." delimiter, you could so something like

=SUBSTITUTE(TRIM(D4&" "&E4&" "&F4&" "&G4&" "&H4), " ", ".")
 
Upvote 0
Maybe this


A
B
C
D
1
Concat Column​
Column1​
Column2​
Column3​
2
Value1​
Value1​
AA​
3
Value1.Value2​
Value1​
Value2​
4
Value2​
AA​
Value2​
5
Value1.Value3​
Value1​
Value3​
6
Value1.Value3​
Value1​
AA​
Value3​

Formula in A2 copied down
=TRIM(SUBSTITUTE(" "&CONCATENATE(IF(LEN(B2)>2,B2,""),IF(LEN(C2)>2,"."&C2,""),IF(LEN(D2)>2,"."&D2,""))," .",""))

Hope this helps

M.
 
Upvote 0
Thanks for the replies, I am away from my computer for right now so I can't test out the solutions. There are spaces in the data so I don't think the substitute solution would work out. Also it is very possible to have a column or multiple columns without data.
 
Upvote 0
This is a particularly ugly version that works with spaced values
=D1&REPT("_",COUNTA(E1)*SIGN(COUNTA($D1:D1))) & E1&REPT("_",COUNTA(F1)*SIGN(COUNTA($D1:E1))) & F1&REPT("_",COUNTA(G1)*SIGN(COUNTA($D1:F1))) & G1&REPT("_",COUNTA(H1)*SIGN(COUNTA($D1:G1))) & H1
 
Upvote 0
Can you make use of a UDF (user defined function) which will work no matter how many columns of data there are? If so...
Code:
[table="width: 500"]
[tr]
	[td]Function ConcatValues(HorzRng As Range) As String
  ConcatValues = Replace(Application.Trim(Join(Evaluate(Replace("IF(ISNUMBER(@),@,"""")", "@", HorzRng.Address(0, 0))))), " ", ".")
End Function[/td]
[/tr]
[/table]

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 ConcatValues just like it was a built-in Excel function. For example, pass in a horizontal range of cells (say, B2:F2) and only the numbers in the range will be concatenated together with a dot delimiter

=ConcatValues(B2:F2)

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.
 
Upvote 0
Can you make use of a UDF (user defined function) which will work no matter how many columns of data there are? If so...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function ConcatValues(HorzRng As Range) As String
  ConcatValues = Replace(Application.Trim(Join(Evaluate(Replace("IF(ISNUMBER(@),@,"""")", "@", HorzRng.Address(0, 0))))), " ", ".")
End Function[/TD]
[/TR]
</tbody>[/TABLE]

Rick,
Thank you for your reply. I attempted to use the code you supplied, but it generated an error. I don't fully understand what the code was trying to do so I tweaked it slight, but didn't get the appropriate answer.

I have a few things going on with my data:
- Everything is either a text field or blank
- Every non-blank field has either a code (2 char long) or a literal name
- I am trying to ignore the codes that were left

So I tweaked the code slightly to try and ignore values that weren't greater than 2 characters, although now it is just getting everything, adding Zero's for any blank fields, and putting spaces in between all my values.

Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function ConcatValues(HorzRng As Range) As String
   ConcatValues = Replace(Application.Trim(Join(Evaluate(Replace("IF(ISNUMBER(@),@,"""")", "@", HorzRng.Address(0, 0))))), " ", ".")
End Function[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Speciality ConcatSpecialtyCode1SpecialtyCode2SpecialtyCode3
SC.0.0.0.0SC
Family.Medicine.0.0.0.0Family Medicine
Pediatrics.0.0.0.0Pediatrics
Family.Medicine.EE.0.0.0Family MedicineEE
Pediatrics.0.0.0.0Pediatrics
Pediatrics.0.0.0.0Pediatrics
Family.Medicine.0.0.0.0Family Medicine
0.0.Family.Medicine.0.0Family Medicine

<tbody>
</tbody>
 
Upvote 0
This is a particularly ugly version that works with spaced values
=D1&REPT("_",COUNTA(E1)*SIGN(COUNTA($D1:D1))) & E1&REPT("_",COUNTA(F1)*SIGN(COUNTA($D1:E1))) & F1&REPT("_",COUNTA(G1)*SIGN(COUNTA($D1:F1))) & G1&REPT("_",COUNTA(H1)*SIGN(COUNTA($D1:G1))) & H1

Mike,

I am looking at this code and it looks like this is just concatenating the values. One of my challenges is I am trying to add a delimiter between the values that I want to keep. I am going to mock up some better data and add it to my initial question. Right now I could have a field that contains a string literal, contains a code (2 characters), or is blank. I am wanting to ignore all the blanks and code fields and add a delimiter (.) only when I have two string literal values that I am concatenating together.
 
Upvote 0
Mike,

I am looking at this code and it looks like this is just concatenating the values. One of my challenges is I am trying to add a delimiter between the values that I want to keep. I am going to mock up some better data and add it to my initial question. Right now I could have a field that contains a string literal, contains a code (2 characters), or is blank. I am wanting to ignore all the blanks and code fields and add a delimiter (.) only when I have two string literal values that I am concatenating together.

Did u try this


=IF(SUM(LEN(D4&E4&F4&G4&H4))>10,RIGHT(IF(LEN(D4)>2,"."&D4,"")&IF(LEN(E4)>2,"."&E4,"")&IF(LEN(F4)>2,"."&F4,"")&IF(LEN(G4)>2,"."&G4,"")&IF(LEN(H4)>2,"."&H4,""),LEN(IF(LEN(D4)>2,"."&D4,"")&IF(LEN(E4)>2,"."&E4,"")&IF(LEN(F4)>2,"."&F4,"")&IF(LEN(G4)>2,"."&G4,"")&IF(LEN(H4)>2,"."&H4,"")) - 1),"")


Val.Val1.VBB.CCC Val Val1 AA VBB CCC
Val.Val.Val AA AA Val Val Val
Val.Val.Val AA Val AA Val Val
 
Upvote 0

Forum statistics

Threads
1,215,492
Messages
6,125,116
Members
449,206
Latest member
burgsrus

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