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?
在单元格中替换换行似乎有效 - =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. Then Trim it all to look good. Alter as you see fit. I also made one that removes All Line Breaks, if interested. 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). This removes all non-printing characters, and works in Mac versions of 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.