简体   繁体   English

将一个单元格拆分为不同数量的单元格-Excel

[英]Split a cell into varying numbers of cells - Excel

Apologies if a similar question has been answered. 抱歉,如果您回答了类似的问题。 I did search, but could not find what I was looking for. 我做了搜索,但是找不到我想要的东西。

I have a spreadsheet into which I have copied and pasted an amount of data about various types of beer. 我有一个电子表格,其中已复制并粘贴了大量有关各种啤酒的数据。 I want to split strings single cells containing text into an arbitrary number of cells corresponding to the type(s) of beer and the alcohol percentage. 我想将包含文本的字符串单个单元格拆分为任意数量的单元格,这些单元格对应于啤酒的类型和酒精百分比。 The thing I'm having trouble with is that some beers have two or more categories, whereas most have only one. 我遇到的麻烦是,有些啤酒有两种或两种以上,而大多数啤酒只有一种。

This is an example of the text strings I'm trying to split: 这是我尝试拆分的文本字符串的示例:

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

The intended result is: 预期的结果是:

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

For beers with one category only I've been using the following formula: 对于只有一种类别的啤酒,我一直使用以下公式:

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

However, I'd like something that will work no matter how many categories the beer has. 但是,无论啤酒有多少种类别,我都希望它能起作用。 I feel like it must be possible seeing as there is a common separator (/). 我觉得一定有可能看到一个公共的分隔符(/)。

Can anyone help? 有人可以帮忙吗?

Let's say your workbook looks like this 假设您的工作簿如下所示

在此处输入图片说明

Try this code. 试试这个代码。 I have commented the code so that you will not have a problem understanding it. 我已经注释了该代码,以便您在理解它时不会遇到问题。

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 OUTPUT

在此处输入图片说明

Instead of using a macro, you can also use Text-to-column option which is inbuilt in Excel. 除了使用宏,您还可以使用Excel中内置的“文本到列”选项。 Goto Datatab-> Text-to-columns 转到数据标签->文本到列

A dialog box will pop out. 将会弹出一个对话框。 In that select delimited radio button and click next. 在那选择分隔的单选按钮,然后单击下一步。 Then specify the delimiter by which you wanna separate the text and click next and finish. 然后指定要用来分隔文本的定界符,然后单击下一步并完成。 You would get your desired result. 您会得到想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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