打破一个单元格,如果它包含一个换行符

在Excel中,如果包含换行符(alt + Enter),我希望能够将一个单元格自动分解为两个或多个单元格。 我如何做到这一点,以便它将细胞分成新的单元格下面的行?

Sub MakeTwoCellsForCellHavingLF() Dim currentCellValue As String, LFFoundAt As Integer currentCellValue = ActiveCell.Value LFFoundAt = InStr(1, currentCellValue, vbLf) If LFFoundAt <> 0 Then ActiveCell.Value = Left(currentCellValue, LFFoundAt - 1) ActiveCell.Offset(1).Value = Mid(currentCellValue, LFFoundAt + 1) End If End Sub 

假设你的数据在A1中。

A2应该包含(请原谅和删除C风格的评论。):

 =FIND(CHAR(10),A1) // Location of CHAR(10), your newline. 

ASCII 10表示换行符。 隐藏第2行

A3应该包含:

 =IF( NOT(ISERR(A2)), // Make sure there is a newline LEFT(A1, A2-1), // Everything up to the newline A1 // If there's no newline, original string ) 

A4应该包含:

 =IF( NOT(ISERR(A2)), // Make sure there is a newline RIGHT(A1, LEN(A1)-A2), // Everything after the newline "" // If there's no newline, nothing )