需要将数据拆分为多列
问题描述:
我在一列中有数据,我想将它分成多列。 比如我有一个像需要将数据拆分为多列
123*4546.765,4653:342.324
数据我想在一列拆分此为123
,在一列4546
,765
一列中,4653
在列等等......
答
尝试下面的代码(使用RegEx
)。
RegEx
模式正在寻找任何连续的1到10位数.Pattern = "[0-9]{1,10}"
。
然后,如果至少找到1个匹配,则在所有匹配内部循环并输出它们(从单元格“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 Sub
你是如何想它可以做还是怎么做。我想你不得不使用Range.TextToColumns方法。 Tab,逗号,分号和**一个**其他字符。 VBA中的一个非平凡的任务,所以你可以通过录制一些宏开始,然后在IDE中完善它们。当你有一些不太有用的实质性代码时,发布一个问题。 –