MS Excel-通过定界符将文本分成特定的列

时间:2019-11-09 12:41:24

标签: excel vba excel-formula spreadsheet

在MS Excel中,我需要用逗号分隔文本,从右边提取三件事,然后将它们放在三个不同的列中,即城市,州和国家。

问题:在原始文本(即完整地址)中,有时我仅获得国家或州的信息。另外,完整地址的长度也有所不同。

Here's a screenshot of what I intend to do

这是实际的spreadsheet

3 个答案:

答案 0 :(得分:3)

根据屏幕快照假定保存在A1:A6中的数据

C1中,公式上下复制:

=IFERROR(TRIM(MID(SUBSTITUTE(","&$A1,",",REPT(" ",399)),(LEN($A1)-LEN(SUBSTITUTE(($A1),",",""))+COLUMN(A1)-2)*399,399)),"")

答案 1 :(得分:1)

如果您的Excel 2013+具有FILTERXML功能,则可以使用:

=IFERROR(FILTERXML("<t><s>" & SUBSTITUTE($A1,",","</s><s>") & "</s></t>","//s[last()-3+" & COLUMNS($A:A) & "]"),"")

上下填充。

  • 创建一个XML,每个逗号分隔的字符串是一个 node
  • 提取最后3个节点,如果不存在则返回""

enter image description here

答案 2 :(得分:0)

首先在标准模块中输入以下三个用户定义的功能:

Public Function LastWord(s As String) As String
    Dim arr
    s = ", , , , , " & s
    arr = Split(s, ", ")
    LastWord = arr(UBound(arr))
End Function

Public Function NLastWord(s As String) As String
    Dim arr
    s = ", , , , , " & s
    arr = Split(s, ", ")
    NLastWord = arr(UBound(arr) - 1)
End Function

Public Function NNLastWord(s As String) As String
    Dim arr
    s = ", , , , , " & s
    arr = Split(s, ", ")
    NNLastWord = arr(UBound(arr) - 2)
End Function

然后在 E1 中输入:

=lastword(A1)

并向下复制。在 D1 中输入:

=NLastWord(A1)

并向下复制。在 C1 中输入:

=NNLastWord(A1)

并向下复制:

enter image description here