将细胞分成不同数量的细胞 – Excel

道歉,如果一个类似的问题已被回答。 我search了,但找不到我在找什么。

我有一个电子表格,我已经复制并粘贴了各种types的啤酒数据。 我想把包含文本的单个单元格的string拆分成与啤酒的types和酒精百分比相对应的任意数量的单元格。 我遇到的麻烦是一些啤酒有两个或更多的类别,而大多数只有一个。

这是我想要分割的文本string的一个例子:

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)) 

不过,无论啤酒有多less种类,我都希望能有所作为。 我觉得有必要可以看到有一个共同的分隔符(/)。

谁能帮忙?

假设你的工作簿是这样的

在这里输入图像说明

试试这个代码。 我已经评论了代码,以便您不会理解它。

 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 

OUTPUT

在这里输入图像描述

您也可以使用Excel中内置的“文本到列”选项来代替使用macros。 转到Datatab->文本到列

popup一个对话框。 在select分隔的单选button,然后单击下一步。 然后指定要分隔文本的分隔符,然后单击下一步并完成。 你会得到你想要的结果。