[英]Excel - how to split a list in a cell into separate cells
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的情况下执行此操作的方法:
=TEXTJOIN()
to get all values combined like so =TEXTJOIN()
可以将所有值组合在一起,如下所示 =TEXTJOIN(CHAR(10,TRUE,$A$2:$A$5)
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.