简体   繁体   English

Excel - 如何将单元格中的列表拆分为单独的单元格

[英]Excel - how to split a list in a cell into separate cells

I am trying to split these names up so that are in a cell each.我试图将这些名称分开,以便每个名称都在一个单元格中。 I have tried text to columns and that doesn't do the job.我曾尝试将文本添加到列中,但这不起作用。

在此处输入图片说明

you can use the code below to split the string by chr(10), which presents a line feed character. 您可以使用下面的代码用chr(10)分割字符串,该字符串表示换行符。 Please note the assumption behind input data header and output column. 请注意输入数据标题和输出列后面的假设。 Please also note that dictionary works in this case only because dictionaries in VBA somehow maintains ordering - this is unlikely to be the case in other programming languages. 另请注意,在这种情况下,词典仅在VBA中以某种方式保持顺序时才起作用-在其他编程语言中则不太可能。

Public Sub sub_test()

    Dim wsThis As Worksheet: Set wsThis = ActiveSheet
    Dim vData As Variant
    Dim dicOutput As Object: Set dicOutput = CreateObject("scripting.dictionary")
    Dim vTemp As Variant
    Dim vLine As Variant
    Dim i As Long


    With wsThis
        ' Read data into memory - assume no header
        vData = .Range(.Range("A1"), .Range("A1").End(xlDown))

        ' Loop through each row
        For i = LBound(vData, 1) To UBound(vData, 1)
            ' Split by new line
            vTemp = Split(vData(i, 1), Chr(10))
            For Each vLine In vTemp
                ' Check if new line is empty string
                If Trim(vLine) <> vbNullString Then
                    dicOutput(vLine) = 1
                End If
            Next vLine
        Next i
        vTemp = Application.Transpose(dicOutput.keys)

        ' Output into worksheet - assume column C
        .Range("C1").Resize(UBound(vTemp, 1) - LBound(vTemp, 1) + 1, UBound(vTemp, 2) - LBound(vTemp, 2) + 1) = vTemp
    End With



End Sub

Here is a way to do this without VBA in Excel 2016: 这是在Excel 2016中不使用VBA的情况下执行此操作的方法:

  • Use =TEXTJOIN() to get all values combined like so 使用=TEXTJOIN()可以将所有值组合在一起,如下所示

=TEXTJOIN(CHAR(10,TRUE,$A$2:$A$5)

  • Now we have the full textstring we want to combine this with a formula to get parts from this string that are between the CHAR(10) linebreaks. 现在我们有了完整的文本字符串,我们希望将其与公式结合使用,以从该字符串中获得CHAR(10)换行符之间的部分。 So in cell B2 you can write: 因此,在单元格B2您可以编写:

=TRIM(MID(SUBSTITUTE(TEXTJOIN(Chr(10,TRUE,$A$2:$A$5),CHAR(10),REP(" ",99)),(ROW()-1)*99-98,99))

Drag down the formula.... 向下拖动公式。

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

相关问题 Excel按字母/数字将单元格拆分为2个单独的单元格 - Excel split cell into 2 separate cells by letters/digits Excel:如何将一个单元格的内容分为三个不同的单元格 - Excel: How to separate the content of a cell into 3 different cells 如何将汇总列表从一个单元格分离为 Excel 中的单独单元格 - How do I separate a aggregate list from One Cell Into Separate Cells In Excel Excel公式-如何将一个单元格中的公式值分为2个不同的单元格? - Excel formula - How to separate a formula value in a cell into 2 different cells? 如何在Excel中将一个单元格的值拆分为多个单元格? - How to split value of one cell into multiple cells in Excel? 如何使用java将一个excel单元格拆分为两个单元格 - How to split one excel cell into two cells using java Excel将数据从1个单元格拆分为多个单元格 - Excel Split Data from 1 cell into multiple cells 将一个单元格拆分为不同数量的单元格-Excel - Split a cell into varying numbers of cells - Excel 将 Excel 单元格中的单词拆分为单独的列 - Split a word in an Excel cell into separate columns VBA 代码使每个值从 excel 单元格中的多选相关下拉列表中选择在单独的相邻单元格中 - VBA code to have each values selected from a multiselect dependent dropdown list in excel cell in separate adjacent cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM