拆分

2017-08-07 43 views
0

我在Excel中按以下格式列在Excel VBA中的號碼和文字:拆分

Column A 
100 Test A 
200 Test B 
300 Test C 

我想打開Excel並分別分裂數字和文本分成兩列,比如:

Column A Column B 
100   Test A 
200   Test B 
300   Test C 

任何人都可以幫助我解決這個問題。

+0

可以使用(例如)'斯普利特(cellValue, 「」,1)'你嘗試過什麼? –

+0

@TimWilliams被分成2016年的功能? –

+1

不,它是在VBA中,因爲Office 2000我認爲(Q用VBA標記) –

回答

3

我覺得你過於複雜的事情:

Option Explicit 
Sub SplitCells() 
Dim totRange As Range 
Dim r As Range 
Dim splitted() As String 

Set totRange = Range("A1:A3") 
For Each r In totRange 
    splitted = Split(r.Value, " ") 
    r.Value = splitted(0) 
    r.Offset(0, 1).Value = splitted(1) & " " & splitted(2) 
Next r 
End Sub 
+0

是的,這爲我提供了相同的解決方案,並且使過程變得簡單。謝謝 :) –

0

正如Tim Williams所建議的,Split功能可能是最好的。如果你不想使用VBA,你可以嘗試內置函數Text to Columns。 根據定界符等,這將產生3列: enter image description here

enter image description here

+0

不錯的選擇,但是之後需要返回與信件結合的測試。如果所有數字大致相同,您可以始終使用非分隔方法。 –

2

非VBA選項

對於塔B

=--left(A1,find(" ",A1)-1) 

和C列

=right(A1,len(A1)-find(" ",A1)) 

根據需要複製這些內容。如果出於某種原因,您希望將該數字保留爲文本而不是轉換爲數字,請從第一個等式中刪除 - 。在方程末尾,將數字轉換爲數字的其他一些方法是+​​0或* 1。基本上把數字作爲文本通過數學運算,並將excel轉換爲數字。

1

感謝您的幫助。我有一個很好的解決方案。

Private Sub SplitData() 
    Dim sourceSheetName As String 
    Dim sourceSheet As Worksheet 
    Dim lastRow As Long 
    Dim uboundMax As Integer 
    Dim result 

    On Error GoTo SplitterErr 
    sourceSheetName = "TestSheet" 
    If sourceSheetName = "" Then _ 
     Exit Sub 

    Set sourceSheet = Worksheets(sourceSheetName) 

    With sourceSheet 
     lastRow = .Range(sourceColumnName & .Rows.Count).End(xlUp).Row 
     result = SplittedValues(data:=.Range(.Cells(1, sourceColumnName), _ 
              .Cells(lastRow, sourceColumnName)), _ 
           partsMaxLenght:=uboundMax) 

     If Not IsEmpty(result) Then 
      .Range(.Cells(1, sourceColumnName), _ 
        .Cells(lastRow, uboundMax)).value = result 
     End If 
    End With 

SplitterErr: 
    If Err.Number <> 0 Then _ 
     MsgBox Err.Description, vbCritical 
End Sub 

Private Function SplittedValues(_ 
    data As Range, _ 
    ByRef partsMaxLenght As Integer) As Variant 

    Dim r As Integer 
    Dim parts As Variant 
    Dim values As Variant 
    Dim value As Variant 
    Dim splitted As Variant 

    If Not IsArray(data) Then 
     ' data consists of one cell only 
     ReDim values(1 To 1, 1 To 1) 
     values(1, 1) = data.value 
    Else 
     values = data.value 
    End If 

    ReDim splitted(LBound(values) To UBound(values)) 

    For r = LBound(values) To UBound(values) 

     value = values(r, 1) 
     If IsEmpty(value) Then 
      GoTo continue 
     End If 

     ' Split always returns zero based array so parts is zero based array 
     parts = VBA.Split(value, delimiter) 
     splitted(r) = parts 

     If UBound(parts) + 1 > partsMaxLenght Then 
      partsMaxLenght = UBound(parts) + 1 
     End If 

continue: 
    Next r 

    If partsMaxLenght = 0 Then 
     Exit Function 
    End If 

    Dim matrix As Variant 
    Dim c As Integer 
    ReDim matrix(LBound(splitted) To UBound(splitted), _ 
       LBound(splitted) To partsMaxLenght) 

    For r = LBound(splitted) To UBound(splitted) 
     parts = splitted(r) 
     For c = 0 To UBound(parts) 
      matrix(r, c + 1) = parts(c) 
     Next c 
    Next r 

    SplittedValues = matrix 


End Function 

但這段代碼的輸出結果如下:

Column A Column B Column C 
100   Test  A 
200   Test  B 
300   Test  C 

所以仍然是這方面的工作。如果出現問題,請告訴我。由於