将xl字段解析为名字、姓氏和电话号码

  • 本文关键字:电话号码 字段 xl excel vba
  • 更新时间 :
  • 英文 :


在xl中,我有一个字段是姓,名字电话号码。我需要将字符串解析为3个字段:LastName;FirstName;和电话号码。名称字段用逗号分隔。我可以解析这些名字,但更复杂的是,电话号码有时是8个字符(3+短划线+3)(Koshevoy,Oleg 345-5387),有时是12个字符(3+1短划线+3+1短划线+4)(Cooke,Lisa 239-980-6688)。如果字段是8个字符,我需要在它&a"-"。

在三列中,最终结果应该看起来像"Robert"Smith"941-525-2752"。你能帮忙吗?

试试barrowc:的想法

Sub test()
i = 1
While Not IsEmpty(Cells(i, 1)) 'search until column A is not empty
lastblank = InStrRev(Cells(i, 1), " ", , vbTextCompare) 'find the last blank
phone = Mid(Cells(i, 1), lastblank + 1, 12) 'take the text (e.g. 12) after the last blank (without the blank)
If Len(phone) = 8 Then
phone = "941-" + phone 'add if phone length = 8
End If
ActiveSheet.Cells(i, 2).Value = phone 'copy to column B
i = i + 1
Wend
End Sub

最新更新