【发布时间】:2019-09-23 07:41:45
【问题描述】:
我在一个单元格中有 50,000 个姓名和地址,我想将每个元素提取到单独的单元格中,但是一个特定的区域会引起头痛。一些地址字段包含街道地址的两个数字。例如,一个单元格可能包含“Mr Smith 10 Rillington Place Exeter Devon”,这很好,因为我的代码将名称街道城市和县分开。但有些牢房包含“Smith & Sons Ltd. 10 & 12 High Street Sutton Surrey。
我尝试过.Pattern = "(\d+|\D+|\[d+ & \d+])",但这与.Pattern = "(\d+|\D+)" 的作用相同
Sub SplitTextNum2()
Dim r As Range, rC As Range
Dim Match, Matches
Dim matchCount As Integer
Columns("E").SpecialCells (xlCellTypeBlanks)
On Error Resume Next
Set r = Range("E1", Range("E50").End(xlDown))
With CreateObject("VBScript.RegExp")
.Pattern = "(\d+|\D+)"
.Global = True
For Each rC In r
Set Matches = .Execute(rC.Value)
For matchCount = 1 To Matches.Count
rC.Offset(, matchCount).Value = Matches(matchCount - 1)
Next
Next rC
End With
End Sub
我的 VBA 宏将“10”“&”“12”拆分为单独的单元格。在这种情况下,我希望它把“10 和 12”放在一个单元格中。该代码还需要满足单个街道号码。任何建议都会有很大帮助。
【问题讨论】:
标签: regex excel vba excel-2007