How to Remove Line Break in String

前端 未结 13 2164
旧时难觅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: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
    

提交回复
热议问题