2013-11-25 36 views
2

道歉,如果類似的問題已經有了答案。我搜索了,但找不到我正在尋找的東西。拆分單元格,進入細胞的不同號碼 - Excel中

我有進我所複製並粘貼約各種類型的啤酒的數據量的電子表格。我想將包含文本的單個單元格拆分成與啤酒類型和酒精百分比相對應的任意數量的單元格。我遇到的問題是一些啤酒有兩個或更多的類別,而大多數只有一個。

這是文本字符串我試圖分裂的例子:

American Pale Ale (APA) / 6.40% ABV 
Quadrupel (Quad) / 10.20% ABV 
American Double/Imperial IPA / 8.00% ABV 
American Wild Ale / 7.75% ABV 

預期的結果是:

Category 1    | Category 2 | Alcohol % 

American Pale Ale (APA) |    | 6.40% ABV 
Quadrupel (Quad)  |    | 10.20% ABV 
American Double   | Imperial IPA | 8.00% ABV 
American Wild Ale  |    | 7.75% ABV 

對於一個類別的啤酒只有我一直在使用下面的公式:

=RIGHT(D3,LEN(D3)-SEARCH("/",D3,1)) 

但是,我想要的東西,將工作,無論有多少類別th啤酒有。我覺得必須有可能看到有一個共同的分隔符(/)。

任何人都可以幫忙嗎?

+0

你的意思是你可以有類似'CAT1/CAT2 /的Cat3/3,4,5/CAT5/X.XX%' –

+0

是的,這正是我的意思。 –

+0

Excel-VBA或公式?我可以立即想到一個VBA解決方案 –

回答

3

比方說,您的工作簿看起來像這樣

enter image description here

試試這個代碼。我已經評論了該代碼,以便您不會理解它。

Option Explicit 

Sub Sample() 
    Dim MYAr 
    Dim ws As Worksheet, wsOutput As Worksheet 
    Dim Lrow As Long, i As Long, j As Long, rw As Long, SlashCount As Long, col As Long 

    '~~> Set this to the relevant worksheet 
    Set ws = ThisWorkbook.Sheets("Sheet1") 

    With ws 
     '~~> get the last row 
     Lrow = .Range("A" & .Rows.Count).End(xlUp).Row 

     '~~> Count the max number of "/" in a particular cell 
     '~~> This will decide the span of the columns 
     For i = 1 To Lrow 
      MYAr = Split(.Range("A" & i).Value, "/") 
      If UBound(MYAr) + 1 > SlashCount Then SlashCount = UBound(MYAr) + 1 
     Next i 

     '~~> Add a new worksheet for output 
     Set wsOutput = ThisWorkbook.Sheets.Add 
     rw = 1 

     For i = 1 To Lrow 
      MYAr = Split(.Range("A" & i).Value, "/") 

      col = 1 
      '~~> Write values to column. We will not write the 
      '~~> Last value of the array here 
      For j = LBound(MYAr) To (UBound(MYAr) - 1) 
       wsOutput.Cells(rw, col).Value = MYAr(j) 
       col = col + 1 
      Next j 
      '~~> Write Last value of the array to the last column 
      wsOutput.Cells(rw, SlashCount).Value = MYAr(UBound(MYAr)) 
      rw = rw + 1 
     Next i 
    End With 
End Sub 

輸出

enter image description here

+0

這太棒了。謝謝! –

1

而不是使用一個宏觀的,你也可以使用文本到列選項,這是在Excel中內置。 轉到Datatab->文本到列

一個對話框會彈出。在該選擇分隔單選按鈕,然後單擊下一步。 然後指定您想要分隔文本的分隔符,然後單擊下一步並完成。 你會得到你想要的結果。

+0

非常好!應該是被接受的答案。 – Noir

相關問題