如果我的字符串以换行符结尾,我想从字符串中删除换行符。
Sub linebreak(myString)
If Len(myString) <> 0 Then
If Right$(myString, 1) = vbCrLf Or Right$(myString, 1) = vbNewLine Then myString = Left$(myString, Len(myString) - 1)
End If
End Sub
str = Replace(str, vbLf, "")
此代码将所有换行符从代码中取出
如果你只想要最后一个:
If Right(str, 1) = vbLf Then str = Left(str, Len(str) - 1)
是你如何尝试确定的方式。
换行 = ASCII10
,换页 = ASCII12
和回车 = ASCII13
。在这里,我们清楚地看到了我们都知道的:PC 来自(电动)打字机。
vbLf
is Chr (10)
and 表示光标向下跳一行(打字机:转动滚轮)
vbCr
is Chr (13)
and 表示光标跳到开头(打字机:拉回卷)
在DOS中,换行符总是VBCrLf
或Chr (13) & Chr (10)
,无论如何在文件中,但例如在 VB 中的文本框也是如此。
另一方面,在Excel 单元格VBLf
中,换行符仅为,即使没有 ,第二行也会从第一个位置开始vbCr
。然后再深入一格。vbCrLf
所以这取决于你从哪里阅读和获取你的字符串。如果要删除字符串中的所有vbLf
( Chr(10)
) 和vbCr
( Char(13)
),可以这样做:
strText = Replace(Replace(strText, Chr(10), ""), Chr(13), "")
如果你只想删除最后一个,你可以像这样测试:
If Right(str, 1) = vbLf or Right(str, 1) = vbCr Then str = Left(str, Len(str) - 1)
vbCrLf
并且vbNewLine
实际上是两个字符长,所以改为Right$(myString, 2)
Sub linebreak(myString)
If Len(myString) <> 0 Then
If Right$(myString, 2) = vbCrLf Or Right$(myString, 2) = vbNewLine Then
myString = Left$(myString, Len(myString) - 2)
End If
End If
End Sub
尝试以下行:
CleanString = Application.WorksheetFunction.Clean(MyString)
当您使用 Excel 时,您不需要 VBA 来实现这一点,您可以简单地使用内置的“Clean()”函数,这将删除回车、换行等,例如:
=Clean(MyString)
可以通过这种方式从 VBA 调用 Clean 函数:
Range("A1").Value = Application.WorksheetFunction.Clean(Range("A1"))
然而,正如这里所写,CLEAN 函数旨在从文本中删除 7 位 ASCII 代码(值 0 到 31)中的前 32 个非打印字符。在 Unicode 字符集中,还有其他非打印字符(值 127、129、141、143、144 和 157)。CLEAN 函数本身不会删除这些额外的非打印字符。
Rick Rothstein 已经编写了代码来处理这种情况:
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
只是这个:
str = Replace(str, vbCrLf, "")
mystring = Replace(mystring, Chr(10), "")
替换(yourString,vbNewLine,“”,,,,vbTextCompare)
我知道这篇文章很旧,但我找不到任何可以做到这一点的东西。所以我终于把所有论坛的这个放在一起。
这将删除左右所有换行符,并删除所有空白行。然后修剪一切看起来不错。改变你认为合适的。如果有兴趣,我还制作了一个删除所有换行符的。泰
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
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
没有其他答案(除了一个例外,请参阅我的编辑)处理有多个尾随回车的情况。如果目标是创建一个类似于“Trim”的函数来删除回车符和空格,那么您可能希望它足够健壮,可以删除尽可能多的,而不仅仅是一个。另外,我建议避免在 VBA 中使用“左”或“右”函数,因为它们在 VB.Net 中不存在。有时可能需要将 Office VBA 宏转换为 VSTO COM 加载项,因此避免使用仅存在于 VBA 中的函数是一个好习惯。
Function RemoveTrailingWhiteSpace(s As String) As String
RemoveTrailingWhiteSpace = s
Dim StrLen As Long
StrLen = Len(RemoveTrailingWhiteSpace)
While (StrLen > 0 And (Mid(RemoveTrailingWhiteSpace, StrLen) = vbCr Or Mid(RemoveTrailingWhiteSpace, StrLen) = vbLf) Or Mid(RemoveTrailingWhiteSpace, StrLen) = " ")
RemoveTrailingWhiteSpace = Mid(RemoveTrailingWhiteSpace, 1, StrLen - 1)
StrLen = Len(RemoveTrailingWhiteSpace)
Wend
End Function
编辑:我应该澄清这里列出的另一个答案是从字符串的两端修剪回车符和空格,但它看起来更加复杂。这可以相当简洁地完成。
从来没有人建议过 RegExp 解决方案。所以这里有一个:
Function TrimTrailingLineBreak(pText)
Dim oRE: Set oRE = New RegExp: oRE.Global = True
oRE.Pattern = "(.*?)(\n|(\r\n)){1}$"
TrimTrailingLineBreak = oRE.Replace(pText, "$1")
End Function
它捕获并返回所有内容,直到文本末尾的单个 ( {1}
) 尾随换行 ( \n
) 或回车和换行 ( )。
要删除所有尾随换行符,请更改为.
并删除所有尾随空格(包括换行符)使用.\r\n
$
{1}
*
oRE.Pattern = "(.*?)\s*$"
我希望这会做。否则,可以直接问我
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
我有完全相同的问题。我制作了一个单独的函数,可以在需要时轻松调用:
Function removeLineBreakIfAtEnd(s As String) As String
If Right(s, 1) = vbLf Then s = Left(s, Len(s) - 2)
removeLineBreakIfAtEnd = s
End Function
我发现我只需要检查最后一个字符并执行 -2 来删除换行符。我还发现检查 vbLf 是检测换行符的唯一方法。该函数可以这样调用:
Sub MainSub()
Dim myString As String
myString = "Hello" & vbCrLf
myString = removeLineBreakIfAtEnd(myString)
MsgBox ("Here is the resulting string: '" & myString & "'")
End Sub