如何从VB或公式的Excel单元格中删除空白换行符?

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

在单元格中replace换行似乎可行 – =Substitute(A1, CHAR(10), "")

我知道这个post是非常古老的,但我找不到任何可以做到这一点的东西。 所以我终于把这个来自所有的论坛。

selectCELL或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 

如果你只是想要所有线路rest消失使用这个。

  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 

如果这不需要公式 –

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

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