【问题标题】:How can I remove blank line breaks from an excel cell with VB or a formula?如何使用 VBA 或公式从 Excel 单元格中删除空白换行符?
【发布时间】:2020-08-16 03:04:52
【问题描述】:

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

【问题讨论】:

标签: vba excel


【解决方案1】:

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

【讨论】:

  • 可能包含 Char(13)(即返回)以及换行符。通常的顺序是 Char(13)+Char(10),反之亦然。
  • @Remou 是对的。使用=Substitute(Substitute(A1, CHAR(10), ""), CHAR(13), "") 来防范。
【解决方案2】:

我知道这篇文章已经很老了,但我找不到任何可以做到这一点的东西。所以我终于把所有论坛的这个放在一起。

选择 CELL 或 Range,这将删除左右所有换行符,并删除所有空行。然后修剪一切看起来不错。改变你认为合适的。如果有兴趣,我还制作了一个删除所有换行符的。 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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:
      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
      

      结束子

      【讨论】:

        【解决方案5】:

        如果这不需要公式 -

        在数据右侧添加一个新列(触及最后一列)。 在该列中插入数字 1、2、3 等到数据的最后一行,表示数据的原始顺序。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-05
          • 2018-05-10
          • 1970-01-01
          相关资源
          最近更新 更多