如何使用VBA或公式从Excel单元格中删除空白换行符?

时间:2010-02-23 19:32:56

标签: excel vba excel-vba

我有很多单元格,数据被分成许多行;其中一些行是空白的。是否有VB函数或公式来删除Excel单元格中的空行换行符或所有换行符?

5 个答案:

答案 0 :(得分:6)

在单元格中替换换行似乎有效 - =Substitute(A1, CHAR(10), "")

答案 1 :(得分:1)

我知道这篇文章很老但我找不到任何能做到这一点的东西。所以我终于从所有的论坛中把它放在了一起。

选择CELL或Range,这将从左侧和右侧删除所有换行符,并删除所有空白行。然后修剪一切看起来不错。改变你认为合适的方式。如果有兴趣的话,我还会删除All Line Breaks。 TY

    Sub RemoveBlankLines()
    Application.ScreenUpdating = False
    Dim rngCel As Range
    Dim strOldVal As String
    Dim strNewVal As String

    For Each rngCel In Selection
        If rngCel.HasFormula = False Then
            strOldVal = rngCel.Value
            strNewVal = strOldVal
            Debug.Print rngCel.Address

            Do
            If Left(strNewVal, 1) = vbLf Then strNewVal = Right(strNewVal, Len(strNewVal) - 1)
            If strNewVal = strOldVal Then Exit Do
                strOldVal = strNewVal
            Loop

            Do
            If Right(strNewVal, 1) = vbLf Then strNewVal = Left(strNewVal, Len(strNewVal) - 1)
            If strNewVal = strOldVal Then Exit Do
                strOldVal = strNewVal
            Loop

            Do
            strNewVal = Replace(strNewVal, vbLf & vbLf, "^")
            strNewVal = Replace(strNewVal, Replace(String(Len(strNewVal) - _
                        Len(Replace(strNewVal, "^", "")), "^"), "^", "^"), "^")
            strNewVal = Replace(strNewVal, "^", vbLf)

            If strNewVal = strOldVal Then Exit Do
                strOldVal = strNewVal
            Loop

            If rngCel.Value <> strNewVal Then
                rngCel = strNewVal
            End If

        rngCel.Value = Application.Trim(rngCel.Value)
        End If
    Next rngCel
    Application.ScreenUpdating = True
End Sub

如果您只想让所有换行消失,请使用此功能。

    Sub RemoveLineBreaks()
    Application.ScreenUpdating = False
    Dim rngCel As Range
    Dim strOldVal As String
    Dim strNewVal As String

    For Each rngCel In Selection
        If rngCel.HasFormula = False Then
            strOldVal = rngCel.Value
            strNewVal = strOldVal
            Debug.Print rngCel.Address

            Do

            strNewVal = Replace(strNewVal, vbLf, " ")

            If strNewVal = strOldVal Then Exit Do
                strOldVal = strNewVal
            Loop

            If rngCel.Value <> strNewVal Then
                rngCel = strNewVal
            End If
        End If
        rngCel.Value = Application.Trim(rngCel.Value)
    Next rngCel
    Application.ScreenUpdating = True
End Sub

答案 2 :(得分:0)

使用公式clean = clean(A1)。这将删除所有非打印字符,并适用于Mac版本的Excel。

答案 3 :(得分:0)

Sub TrimEmptyLines()
Dim cel As Range, s As String, len1 As Long, len2 As Long
For Each cel In ActiveSheet.UsedRange
    If Not IsError(cel.Value2) Then
        If InStr(1, cel.Text, vbLf) > 0 Then
            s = Trim(cel.Value2)
            Do ' remove duplicate vbLf
                len1 = Len(s)
                s = Replace$(s, vbLf & vbLf, vbLf)
                len2 = Len(s)
            Loop Until len2 = len1

            ' remove vblf at beginning or at end
            If Left$(s, 1) = vbLf Then s = Right$(s, Len(s) - 1)
            If Right$(s, 1) = vbLf Then s = Left$(s, Len(s) - 1)

            cel.Value = Trim$(s)
        End If
    End If
Next

结束子

答案 4 :(得分:-1)

如果这不需要公式 -

在数据右侧立即添加新列(触摸最后一列)。 将数字1,2,3等插入此列到数据的最后一行,表示数据的原始顺序。

对除此新列之外的任何字段的数据进行排序。 所有空行将分类到数据的顶部或底部。 删除所有连续的空白行。 对您添加的列进行排序以保留原始数据行顺序。 删除您添加的列。