简体   繁体   English

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

[英]How can I remove blank line breaks from an excel cell with VB or a formula?

I have many cells where data is broken up into many lines;我有很多单元格,其中数据被分成多行; some of those lines are blank.其中一些行是空白的。 Is there a VB function or formula to remove blank line breaks in Excel cells, or all line breaks?是否有 VB 函数或公式可以删除 Excel 单元格中的空白换行符或所有换行符?

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

I know this post is very old but I could not find anything that would do this.我知道这篇文章很旧,但我找不到任何可以做到这一点的东西。 So I finally put this together from all the forums.所以我终于把这个从所有论坛放在一起。

Select CELL or Range and this will remove All line breaks from the left and right, and removes any blank lines.选择 CELL 或 Range,这将删除左右所有换行符,并删除所有空行。 Then Trim it all to look good.然后修剪它看起来不错。 Alter as you see fit.根据您的需要进行更改。 I also made one that removes All Line Breaks, if interested.如果有兴趣,我还制作了一个删除所有换行符的内容。 TY 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

If you Just want ALL line Breaks gone use this.如果您只想让所有换行符消失,请使用它。

    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

Use the formula clean eg =clean(A1).使用公式clean 例如=clean(A1)。 This removes all non-printing characters, and works in Mac versions of excel.这将删除所有非打印字符,并适用于 Mac 版本的 excel。

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

End Sub结束子

If this does not need formulas -如果这不需要公式 -

Add a new column immediately to the right of your data (touching the last column).立即在数据右侧添加一个新列(触摸最后一列)。 Insert into this column the numbers 1, 2, 3, etc, to the last row of your data, indicating the original order of the data.将数字 1、2、3 等插入此列到数据的最后一行,表示数据的原始顺序。

Sort the data on any field except this new column.对除此新列之外的任何字段上的数据进行排序。 All the blank rows will be sorted to the top or bottom of the data.所有空白行将被排序到数据的顶部或底部。 Delete all the contiguous blank rows.删除所有连续的空白行。 Sort on the column you added to retain the original data row order.对添加的列进行排序以保留原始数据行顺序。 Delete the column you added.删除您添加的列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从单元格中删除换行符,但将地址格式保留在同一单元格VBA中 - How can I remove line breaks from a cell but keep the address format in the same cell VBA 如何在 Excel 中使用 VBA 根据换行符过滤单个单元格中的行? - How can I use VBA in Excel to filter lines in a single cell based on line breaks? 删除换行符,返回托架以及Excel单元格中的所有前导空格 - Remove line breaks, return carriages, and all leading space in Excel Cell 如何检测其中包含公式的单元格是否为空白? - How to detect if a cell with a formula in it is blank? Excel如何将一个单元格中的换行符提取到单独的列中 - Excel how to extract line breaks in one cell into separate columns 在 Excel 中,如何避免为了检查返回值是否为空白而重复大部分公式? - In Excel, how can I avoid repeating a big part of the formula just to check if the return value is blank? 如何使用换行符将具有换行符的不同单元格拆分为一个单元格(VBA Excel) - How to loop the split of different cells with line breaks into one cell with line breaks (VBA Excel) excel vba - 从基于另一列空白的变体中删除单元格 - excel vba - remove cell from a variant based on blank in another column 是否存在一个可以将一个单元格中的一个字符串到另一个单元格中的所有蓝色字体数字求和的excel公式? - Is there an excel formula that I can use to sum all blue font numbers from a string in one cell to another? 我怎样才能从公式恒定在Excel的VBA? - How can I get constant from formula at Excel VBA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM