如果单元格包含换行符,则打破单元格

时间:2009-08-18 05:30:16

标签: excel

在Excel中,如果单元格包含换行符(alt + Enter),我希望能够将单元格自动分解为2个或更多单元格。我该怎么做才能将细胞分成该行下面的新细胞?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

假设您的数据位于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
   )