How to Remove Line Break in String

前端 未结 13 2094
旧时难觅i
旧时难觅i 2020-12-09 01:16

I want to Remove the Line Break from the string if my string Ends with Line Break.

Sub linebreak(myString)
    If Len(myString) <> 0 Then
        If Rig         


        
相关标签:
13条回答
  • 2020-12-09 01:34

    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.

    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
    
    0 讨论(0)
  • 2020-12-09 01:39

    I had the exact same issue. I made a separate function I can call easily when needed:

    Function removeLineBreakIfAtEnd(s As String) As String
        If Right(s, 1) = vbLf Then s = Left(s, Len(s) - 2)
        removeLineBreakIfAtEnd = s
    End Function
    

    I found that I needed to check the last character only and do -2 to remove the line break. I also found that checking for vbLf was the ONLY way to detect the line break. The function can be called like this:

    Sub MainSub()
        Dim myString As String
        myString = "Hello" & vbCrLf
        myString = removeLineBreakIfAtEnd(myString)
        MsgBox ("Here is the resulting string: '" & myString & "'")
    End Sub
    
    0 讨论(0)
  • 2020-12-09 01:41

    Clean function can be called from VBA this way:

    Range("A1").Value = Application.WorksheetFunction.Clean(Range("A1"))

    However as written here, the CLEAN function was designed to remove the first 32 non-printing characters in the 7 bit ASCII code (values 0 through 31) from text. In the Unicode character set, there are additional nonprinting characters (values 127, 129, 141, 143, 144, and 157). By itself, the CLEAN function does not remove these additional nonprinting characters.

    Rick Rothstein have written code to handle even this situation here this way:

    Function CleanTrim(ByVal S As String, Optional ConvertNonBreakingSpace As Boolean = True) As String
      Dim X As Long, CodesToClean As Variant
      CodesToClean = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
                           21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 129, 141, 143, 144, 157)
      If ConvertNonBreakingSpace Then S = Replace(S, Chr(160), " ")
      For X = LBound(CodesToClean) To UBound(CodesToClean)
        If InStr(S, Chr(CodesToClean(X))) Then S = Replace(S, Chr(CodesToClean(X)), "")
      Next
      CleanTrim = WorksheetFunction.Trim(S)
    End Function
    
    0 讨论(0)
  • 2020-12-09 01:45
    mystring = Replace(mystring, Chr(10), "")
    
    0 讨论(0)
  • 2020-12-09 01:45
    Private Sub Form_Load()
        Dim s As String
    
        s = "test" & vbCrLf & "this" & vbCrLf & vbCrLf
        Debug.Print (s & Len(s))
    
        s = Left(s, Len(s) - Len(vbCrLf))
        Debug.Print (s & Len(s))
    End Sub
    
    0 讨论(0)
  • 2020-12-09 01:50

    just this:

    str = Replace(str, vbCrLf, "")
    
    0 讨论(0)
提交回复
热议问题