VBA仅替换开头和结尾的换行符

扎南

我将以下文本存储在变量中myStringVariable

"


    About this item 
    This fits your . 
    Make sure this fits by entering your model number. 

"

抱歉,我不得不在""上面显示通过VBA宏生成的单元格中的行首和行尾中断。

如何摆脱领先和落后的换行符,以获取以下信息:

    About this item 
    This fits your . 
    Make sure this fits by entering your model number. 

我不确定该如何处理。

编辑:

Replace(myStringVariable, vbNewLine, "")

将替换所有换行符。因此,我需要Trim一种我认为功能性的解决方案。

罗恩·罗森菲尔德

这是一个trimCHAR与ExcelTRIM功能类似的功能,不同之处在于您可以指定要TRIM的字符。

它会删除所有的开头和结尾char,以及任何一倍 char的字符串中(留下一个char字符串中,如TRIM用空格一样)

Function trimCHAR(ByVal S As String, char As String)
'similar to worksheet TRIM function except can specify character(s) to TRIM
  Dim RE As Object
  Dim I As Long

Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = True
    .multiLine = True

'need to do separately, otherwise multiple chars within will
'be removed
        .Pattern = char & "*$"
        S = .Replace(S, "") 'Remove extra chars at end of string
        .Pattern = char & "*([^" & char & "]" & char & ")*"
        S = .Replace(S, "$1") 'Remove extra chars at start of string or within
End With
trimCHAR = S

End Function

原版的

在此处输入图片说明

工作表上的用法: =trimCHAR(cell_ref,CHAR(10))

在宏中,您可以使用 =trimChar(myStringVariable, vblf)

结果

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章