2013-11-25 33 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

相关问题