Excel Text Manipulation II

%3CLINGO-SUB%20id%3D%22lingo-sub-2753061%22%20slang%3D%22en-US%22%3EExcel%20Text%20Manipulation%20II%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753061%22%20slang%3D%22en-US%22%3E%3CP%3EDevice%3A%20PC%2FWindows%2010%3C%2FP%3E%3CP%3EMicrosoft%20Office%20Home%20and%20Business%202013%3C%2FP%3E%3CP%3EExcel%20Product%20ID%3A%2000196-20943-85146-AA714%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDear%20Community%2C%3C%2FP%3E%3CP%3E%26nbsp%3BHere%20is%20my%20second%20posting%20on%20this%20subject.%20By%20myself%20I%20am%20making%20a%20bit%20of%20progress%20in%20the%20way%20I%20think%20I%20need%20to%20go.%20Please%20help%20me%20continue.%3C%2FP%3E%3CP%3E%26nbsp%3BIn%20column%20D%20of%20the%20enclosed%20spreadsheet%20I%20want%20to%20extract%20all%20comma%20delimited%20%E2%80%98words%E2%80%99%20starting%20with%20%E2%80%9Ca-%E2%80%9Cand%20place%20them%20in%20column%20E.%20%26nbsp%3BSimilarly%20I%20want%20to%20place%20all%20%E2%80%98words%E2%80%99%20starting%20with%20%E2%80%9Cb-%E2%80%9C%20in%20column%20F.%3C%2FP%3E%3CP%3E%26nbsp%3BUsing%20excel%20functions%2C%20I%20believe%2Fhope%20that%20a%20program%20can%20be%20written%20to%20accomplish%20my%20goal.%3C%2FP%3E%3CP%3E%26nbsp%3BNote%20that%20the%20%E2%80%98Before%E2%80%99%20tab%20holds%20my%20original%20data%3B%20the%20%E2%80%98After%3B%20holds%20my%20desired%20result%3B%20and%20the%20%E2%80%98Play%E2%80%99%20tab%20shows%20the%20results%20of%20my%20experiments%20with%20excel%20functions.%3C%2FP%3E%3CP%3E%26nbsp%3BHere%20is%20what%20I%20have%20been%20able%20to%20do%20so%20far%3A%3C%2FP%3E%3COL%3E%3CLI%3EUse%20the%20MID%20function%20to%20get%20a%20%E2%80%98word%E2%80%99%20if%20you%20know%20its%20staring%20position%20and%20length.%20For%20instance%20%3DMID(%5B%40Animals%5D%2C22%2C8)%20will%20display%20%E2%80%98a-Beagle%E2%80%99%20in%20column%20E%20Row%202.%3C%2FLI%3E%3CLI%3EUse%20the%20FIND%20function%20within%20the%20MID%20function%20to%20get%20the%20first%20%E2%80%98word%E2%80%99%20starting%20with%20%E2%80%98a-%E2%80%98%3B%20if%20you%20know%20its%20length.%26nbsp%3B%20For%20instance%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3DMID(%5B%40Animals%5D%2CFIND(%22a-%22%2C%5B%40Animals%5D%2C1)%2C16)%20will%20display%20%E2%80%98a-Cocker%20Spaniel%E2%80%99%20Column%20E%20row%205.%3C%2FP%3E%3COL%3E%3CLI%3EUse%20the%20%26nbsp%3BFIND%20function%20within%20the%20MID%20function%20within%20the%20CONCATENATE%20function%20to%20get%20two%20%E2%80%98words%E2%80%99%20if%20you%20know%20the%20length%20of%20first%20word%2C%20a%20position%20after%20the%20first%20%E2%80%98a-%E2%80%98%20and%20before%20the%20second%20%E2%80%98a-%E2%80%98%2C%20and%20the%20length%20of%20the%20second%20%E2%80%98word%E2%80%99.%20For%20instance%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3DCONCATENATE(MID(%5B%40Animals%5D%2CFIND(%22a-%22%2C%5B%40Animals%5D%2C1)%2C8)%2C%22%2C%22%2C(MID(%5B%40Animals%5D%2CFIND(%22a-%22%2C%5B%40Animals%5D%2C38)%2C15)))%20will%20display%20%E2%80%98a-Beagle%2Ca-French%20Poodle%E2%80%99%20in%20column%20E%20row%206.%3C%2FP%3E%3CP%3EThanks%2C%20Joel%20Bauer%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2753061%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2753144%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Text%20Manipulation%20II%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753144%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1148049%22%20target%3D%22_blank%22%3E%40Joel_Bauer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DIFERROR(TEXTJOIN(%22%2C%22%2CTRUE%2CFILTERXML(%22%3CY%3E%3CZ%3E%22%26amp%3BSUBSTITUTE(%5B%40Animals%5D%2C%22%2C%22%2C%22%3C%2FZ%3E%3CZ%3E%22)%26amp%3B%22%3C%2FZ%3E%3C%2FY%3E%22%2C%22%2F%2Fz%5Bcontains(.%2C%20'a-')%5D%22))%2C%22%22)%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2753181%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Text%20Manipulation%20II%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753181%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1148049%22%20target%3D%22_blank%22%3E%40Joel_Bauer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20an%20alternative%2C%20a%20very%20short%20VBA%20function%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3EFunction%20GetAnimals(Animals%20As%20String%2C%20Letter%20As%20String)%0A%20%20%20%20GetAnimals%20%3D%20Join(Filter(Split(Animals%2C%20%22%2C%22)%2C%20Letter%20%26amp%3B%20%22-%22)%2C%20%22%2C%22)%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EUse%20like%20this%3A%3C%2FP%3E%0A%3CP%3ESee%20the%20attached%20version%20(now%20a%20macro-enabled%20workbook).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2753263%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Text%20Manipulation%20II%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753263%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELooks%20like%20a%20solution%20that%20snb%20would%20have%20made.%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2753439%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Text%20Manipulation%20II%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753439%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1639%22%20target%3D%22_blank%22%3E%40Detlef%20Lewin%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYes%2C%20I%20see%20what%20you%20mean!%20%3CGRIN%3E%3C%2FGRIN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2753553%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Text%20Manipulation%20II%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753553%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1148049%22%20target%3D%22_blank%22%3E%40Joel_Bauer%3C%2FA%3E%26nbsp%3BHere%20is%20another%20solution%20not%20as%20elegant%20as%20the%20previous%20but%20may%20help%20give%20you%20some%20ideas%20on%20techniques%20you%20can%20use.%26nbsp%3B%20Here%20is%20the%20formula%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DLET(%20source%2C%5B%40Animals%5D%2C%0A%20%20%20%20%20filter_key%2C%20%22a%22%2C%0A%20%20%20%20%20length%2CLEN(source)%2C%0A%20%20%20%20%20list_expanded%2C%20SUBSTITUTE(source%2C%22%2C%22%2CREPT(%22%20%22%2Clength))%2C%0A%20%20%20%20%20array%2C%20TRIM(MID(list_expanded%2CSEQUENCE(length-LEN(SUBSTITUTE(source%2C%22%2C%22%2C%22%22))%2B1%2C%2C1%2Clength)%2Clength))%2C%0A%20%20%20%20%20filtered%2C%20FILTER(array%2CLEFT(array%2C2)%3D%20filter_key%26amp%3B%22-%22%2C%22%20%22)%2C%0A%20%20%20%20%20TEXTJOIN(%22%2C%20%22%2CTRUE%2Cfiltered))%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2753559%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Text%20Manipulation%20II%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2753559%22%20slang%3D%22en-US%22%3Eoh%20wait.%20I%20just%20notice%20you%20don't%20have%20excel%20365%20so%20forget%20that%20response%20as%20you%20won't%20have%20the%20LET()%20function.%20This%20could%20actually%20be%20converted%20to%20your%20version%20but%20the%20size%20of%20the%20equation%20would%20be%20large%20to%20say%20the%20least.%3C%2FLINGO-BODY%3E
Occasional Contributor

Device: PC/Windows 10

Microsoft Office Home and Business 2013

Excel Product ID: 00196-20943-85146-AA714

 

Dear Community,

 Here is my second posting on this subject. By myself I am making a bit of progress in the way I think I need to go. Please help me continue.

 In column D of the enclosed spreadsheet I want to extract all comma delimited ‘words’ starting with “a-“and place them in column E.  Similarly I want to place all ‘words’ starting with “b-“ in column F.

 Using excel functions, I believe/hope that a program can be written to accomplish my goal.

 Note that the ‘Before’ tab holds my original data; the ‘After; holds my desired result; and the ‘Play’ tab shows the results of my experiments with excel functions.

 Here is what I have been able to do so far:

  1. Use the MID function to get a ‘word’ if you know its staring position and length. For instance =MID([@Animals],22,8) will display ‘a-Beagle’ in column E Row 2.
  2. Use the FIND function within the MID function to get the first ‘word’ starting with ‘a-‘; if you know its length.  For instance

         =MID([@Animals],FIND("a-",[@Animals],1),16) will display ‘a-Cocker Spaniel’ Column E row 5.

  1. Use the  FIND function within the MID function within the CONCATENATE function to get two ‘words’ if you know the length of first word, a position after the first ‘a-‘ and before the second ‘a-‘, and the length of the second ‘word’. For instance

         =CONCATENATE(MID([@Animals],FIND("a-",[@Animals],1),8),",",(MID([@Animals],FIND("a-",[@Animals],38),15))) will display ‘a-Beagle,a-French Poodle’ in column E row 6.

Thanks, Joel Bauer

14 Replies

@Joel_Bauer 

=IFERROR(TEXTJOIN(",",TRUE,FILTERXML("<y><z>"&SUBSTITUTE([@Animals],",","</z><z>")&"</z></y>","//z[contains(., 'a-')]")),"")

@Joel_Bauer 

As an alternative, a very short VBA function:

Function GetAnimals(Animals As String, Letter As String)
    GetAnimals = Join(Filter(Split(Animals, ","), Letter & "-"), ",")
End Function

Use like this:

See the attached version (now a macro-enabled workbook).

@Hans Vogelaar 

Looks like a solution that snb would have made.

 

@Detlef Lewin 

Yes, I see what you mean! <grin>

@Joel_Bauer Here is another solution not as elegant as the previous but may help give you some ideas on techniques you can use.  Here is the formula:

=LET( source,[@Animals],
     filter_key, "a",
     length,LEN(source),
     list_expanded, SUBSTITUTE(source,",",REPT(" ",length)),
     array, TRIM(MID(list_expanded,SEQUENCE(length-LEN(SUBSTITUTE(source,",",""))+1,,1,length),length)),
     filtered, FILTER(array,LEFT(array,2)= filter_key&"-"," "),
     TEXTJOIN(", ",TRUE,filtered))
oh wait. I just notice you don't have excel 365 so forget that response as you won't have the LET() function. This could actually be converted to your version but the size of the equation would be large to say the least.

@Detlef Lewin 

Hi Detlef,

  Thanks for your response, however your formula is not working for me. Very well could be my bad.

   In the enclosed spreadsheet I pasted your formula into the formula bar for Cell E2. The Cell remained blank. Same for Cell E3.

   Can you tell me why it’s not working?

Joel

apparently TEXTJOIN() was introduced in 2019 version

@Joel_Bauer  so here is a version of that FILTERXML you can try:

=IFERROR(CONCATENATE(FILTERXML("<y><z>"&SUBSTITUTE([@Animals],",",", </z><z>")&"</z></y>","//z[contains(., 'a-')]")),"")

the problem is that in my version it gives a #SPILL! error but your version wont have spill.  It works for me if I use CONCAT but like TEXTJOIN those aren't available in your version.  Also, unless the last item is included it will have a hanging comma added on the end.

I also created a version of the formula i did earlier that should work in 2013 but it also has similar issues as the above with concatenate wanting to spill and potential comma at the end:

 

=CONCATENATE(TRIM(MID(SUBSTITUTE([@Animals],",",","&REPT(" ",1000)),ROW(OFFSET(A1,0,0,LEN([@Animals])-LEN(SUBSTITUTE([@Animals],",",""))+1))*1000-999,1000*(LEFT(TRIM(MID(SUBSTITUTE([@Animals],",",REPT(" ",1000)),ROW(OFFSET(A1,0,0,LEN([@Animals])-LEN(SUBSTITUTE([@Animals],",",""))+1))*1000-999,1000)),2)="a-"))))

 

 

Hi Hans,
Thanks so much for your response, but I need some help implementing it.
Unfortunately I do not know how to add your @(Animals) function or any function a .xslm workbook.
Can you tell me or can you point me to some useful documentation?
Also I have an issue with the .xslm workbook you sent me.
I get an “#NAME?” error in column E when I hit return in the formula bar. Is this error due to the fact that the ‘@Animals’ function is not installed in my excel version?
Joel

@Joel_Bauer 

The [@Animals] in the formula is a so-called structured table reference. It points the the cell in the Animals column in the same row.

S0762.png

Did you allow macros when you opened the workbook?

 

See Create custom functions in Excel for info on creating a VBA function.

Hi Mtarler,

Thanks so much for your help.
=IFERROR(CONCATENATE(FILTERXML("<y><z>"&SUBSTITUTE([@Animals],",",", </z><z>")&"</z></y>","//z[contains(., 'a-')]")),"")
This formula is the best so far for me. It did not give an error when I copied it into my version of the workbook, and displayed the first ‘word’ starting with ‘a-‘. But it did not display subsequent ‘words’ starting with ‘a-‘. In your enclosed workbook Cell G2 (in the play tab) should be ‘a-Beagle, a-French Poodle,” not just ‘a-Beagle’. I am hoping we are close to success. Can you see how to fix it? The trailing comma is okay.

Joel
Hans,
Thank you so much.
Now this function is working for me in my 2013 Excel version when I use the suffix .xlsm.
I tried and I guess somehow I allowed macros.
Also I can modify your function a bit as necessary.
Joel
Unfortunately the CONCATENATE() function will not work on arrays. So the FILTERXML() solution will return an array but the CONCATENATE() function will not join them and will only return the 1 in the list. (It does the same with the other equation also but that equation returns a blank for each non-correct entry so you see blanks in each case)
@Hans Vogelaar solution using a macro is your best option if you can't upgrade to a newer Excel. You need to make sure your security allows macros. As he noted, the [@Animals] reference is just a way to reference the corresponding row in the column called "Animals" but to try it out maybe just use the actual cell reference or a text string to make sure it is working:
=GetAnimals(D2,"a") or
=GetAnimals("Elephant,Monkey,Lion,a-Beagle,a-French Poodle,b-Persion,b-Amercan Wirehair","a")