我在一列中有数据,我想将其拆分成多列。例如,我有像这样的数据
123*4546.765,4653:342.324我想将其拆分为列中的123、列中的4546、列中的765、列中的4653,依此类推……
发布于 2017-02-17 17:15:55
尝试下面的代码(使用RegEx)。
RegEx模式查找任意连续的1到10位数字.Pattern = "[0-9]{1,10}"。
然后,如果至少找到一个匹配项,则在所有匹配项中循环并输出它们(从单元格"A1“并向右移动一列)。
代码
Option Explicit
Sub ExtractNumbers()
Dim Col As Long
Dim Reg1 As Object
Dim RegMatches As Variant
Dim Match As Variant
Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
.Global = True
.IgnoreCase = True
.Pattern = "[0-9]{1,10}" ' Match any set of 9 digits
End With
Set RegMatches = Reg1.Execute("123*4546.765,4653:342.324") '<-- you can use a `Range` value instead of hard-code
If RegMatches.Count >= 1 Then '<-- check that at least 1 match made with the RegEx
Col = 1
For Each Match In RegMatches
MsgBox Match ' <-- output in msgbox
Cells(1, Col).Value = Match '<-- output the numbers from Cell A1 and move one column to the right
Col = Col + 1
Next Match
End If
End Subhttps://stackoverflow.com/questions/42293438
复制相似问题