Search code examples
excelrandomexcel-formulatextjoin

Excel random value from a string containing substrings


I have this table that is defined as "Table1".

enter image description here

I also have a string which contains substrings inside it with delimiter ", " (comma + space)

I would like to get a formula to get one of those substrings randomly.

Excel version: Excel 365

Thanks in advance.


Solution

  • We can use FILTERXML to split the string and INDEX with RANDBETWEEN to randomly select one of the items:

    =LET(valarr,FILTERXML("<t><s>"&SUBSTITUTE(A2,",","</s><s>")&"</s></t>","//s"),INDEX(valarr,RANDBETWEEN(1,COUNTA(valarr))))
    

    You can change the A2 to [@string containing substring] for the structured reference.

    enter image description here