简体   繁体   中英

Split multiple cells into column

I have multiple cells with string like

for. ex cel A1

m2_10cm[0.10],m2_20cm[0.20],m2_5cm[0.05],m3[1.9]

and cell A2

m3_22[2.2],m3_19[1.9]

Hov can i split it to one column like

  Column  B
    Cell 1   m2_10cm[0.10]
    Cell 2 m2_20cm[0.20]
    Cell 3 m2_5cm[0.05]
    Cell 4 m3[1.9]
    Cell 5 m3_22[2.2]
    Cell 6 m3_19[1.9]

I will be apriciate for any help

Here is a solution where you need to have every other column hidden: Say that you have the value in A1 then put in the following formulas:

B1: =IF(ISERR(FIND(",";A1;1));A1;LEFT(A1;FIND(",";A1;1)-1))
C1: =IF(ISERR(FIND(",";A1;1));"";RIGHT(A1;LEN(A1)-FIND(",";A1;1)))

B1 will then contain the first value in the list, C1 will contain the list minus the first value. Now you can copy these formulas to D1 and E1 and they will now look like

D1: =IF(ISERR(FIND(",";C1;1));C1;LEFT(C1;FIND(",";C1;1)-1))
E1: =IF(ISERR(FIND(",";C1;1));"";RIGHT(C1;LEN(C1)-FIND(",";C1;1)))

Now keep on copying this formulas for as long to the right as you need.

Once this is done you can hide all columns that contain the shortened list, starting with C, then E, etc.

Using VBA code in Excel:

note: google how to a command button on the sheet and paste this as the code.

Option Explicit

' note: vbNullString is the same as "" (empty string)

Const START_ROW = 1
Const SRC_COL = 1    ' column A
Const DST_COL = 2    ' column B

' this gets triggered when the button is pressed
Private Sub CommandButton1_Click()
    ' call the routine
    Call Go
End Sub

Function Go()
    ' assume the button is on the sheet to be processed
    Dim ws As Excel.Worksheet
    Set ws = Excel.ActiveSheet

    Dim srcRow As Integer ' current row being processed
    Dim dstRow As Integer ' current row to put result in
    srcRow = START_ROW: dstRow = START_ROW

    ' keep going while column 'A' is not blank
    While ws.Cells(srcRow, SRC_COL) <> vbNullString
        Call Split(ws, ws.Cells(srcRow, SRC_COL), dstRow)
        srcRow = srcRow + 1
    Wend
End Function

Sub Split(ws As Excel.Worksheet, srcStr As String, ByRef dstRow As Integer)
    If (srcStr = vbNullString) Then
        'remove comment if you want blanks at the end
        ' ex. Apple,Banana, 
        '     will create 3 entries, notice the comma at the end 
        'ws.Cells(dstRow, DST_COL) = vbNullString
        'dstRow = dstRow + 1
        Exit Sub
    endif

    ' find ","
    Dim pos As Integer
    pos = InStr(1, srcStr, ",")
    If (pos = 0) Then
        ' no "," - put the whole string
        ws.Cells(dstRow, DST_COL) = Trim(srcStr)
        dstRow = dstRow + 1
    Else
        ' has "," - put the left part of the string
        ' ex: apple,banana,carrot
        '     put "apple"
        '     continue processing "banana,carrot"
        ws.Cells(dstRow, DST_COL) = Trim(Mid(srcStr, 1, pos - 1))
        ' move to next row and process the right of the string
        dstRow = dstRow + 1
        Call Split(ws, Mid(srcStr, pos + 1), dstRow)
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM