2017-02-17 167 views
0

我在一列中有數據,我想將它分成多列。 比如我有一個像需要將數據拆分爲多列

123*4546.765,4653:342.324 

數據我想在一列拆分此爲123,在一列4546765一列中,4653在列等等......

+0

你是如何想它可以做還是怎麼做。我想你不得不使用Range.TextToColumns方法。 Tab,逗號,分號和**一個**其他字符。 VBA中的一個非平凡的任務,所以你可以通過錄制一些宏開始,然後在IDE中完善它們。當你有一些不太有用的實質性代碼時,發佈一個問題。 –

回答

0

嘗試下面的代碼(使用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