从格式为“城市,州邮政编码”的文本中解析“城市,州”和“邮政编码”

时间:2020-06-05 22:03:10

标签: excel vba excel-formula

我需要将“邮政编码”中的“城市,州”合并到4,000多个商店地址的列表中(以下小样本)。

我需要做什么才能仅将具有“ City,State Zip”的单元格拆分为两个新列,其中一个包含“ City,State”,另一个包含“ Zip”,而忽略所有其他细胞?

Bel Air     
3436 Bel Air Mall       
Mobile, AL 36606
Bridge Street       
330 The Bridge Street       
Huntsville, AL 35806        
Colonial Mall Auburn        
1627 Opelika Road       
Auburn, AL 36830        
Eastchase       
6850 Eastchase Parkway      
Montgomery, AL 36117        
Eastern Shore Centre        
30500 Highway 181       
Spanish Fort, AL 36527      
Gadsden     
1001 Rainbow Drive      
Gadsden, AL 35901       

1 个答案:

答案 0 :(得分:1)

这里有一些工作代码。假设您的地址在excel工作表列“ A”中。而且它们都遵循与您的示例相同的格式

Sub split_out_zip()

For x = 1 To Range("A" & Rows.Count).End(xlUp).Row
Line = trim(Cells(x, "A"))

If InStr(Line, ",") Then
    zip = Right(Line, 5)
    cityState = Left(Line, Len(Line) - 5)
    Cells(x, "B") = cityState
    Cells(x, "C") = zip

End If

Next x
End Sub

这将在B和C列中输出