在xl中,我有一个姓氏,名字是电话号码的字段。我需要将字符串解析为3个字段:LastName;名字;和PhoneNumber。名称字段用逗号分隔。我可以解析名字,但复杂的是,电话号码有时是8个字符(3 + a dash + 3)(Koshevoy,Oleg 345-5387),有时它是12个字符(3 + 1 dash + 3 + 1)破折号+ 4)(库克,丽莎239-980-6688)。如果该字段是8个字符,我需要在它之前添加941区号。 a" - "。
最终结果应该像" Robert" "史密斯" " 941-525-2752"跨三列。你能帮忙吗?
答案 0 :(得分:0)
尝试使用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