[英]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.