为什么 VBA 替换函数不能与 Word 和 Excel 中的 CRLF 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43876586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why Doesn't VBA replace function work with CRLF in Word and Excel
提问by Mickey D
I could have sworn I have stripped CRLF in the past but not sure why the following isn't working:
我可以发誓我过去已经剥离了 CRLF,但不确定为什么以下内容不起作用:
myString = "ABC" & vbCrLf & "DEF"
str1 = Replace(myString, vbLf, "")
str2 = Replace(str1, vbCrLf, "")
str3 = Replace(str2, vbNewLine, "")
MsgBox str3
The code above doesn't work the result is:
上面的代码不起作用,结果是:
ABC
DEF
myString = "ABC" & vbCrLf & "DEF"
str1 = Replace(myString, Chr(13), "")
str2 = Replace(str1, Chr(10), "")
MsgBox str2
The code above does work the result is:
上面的代码确实有效,结果是:
ABCDEF
Solution: Thanks @ Mat for the answer (The problem on the first code was the order I was trying to remove the items) VbCrLf & VbNewLine is the same and trying to remove the combo vbCr+VbLf after removing VbLf won't work
解决方案:感谢@ Mat 的回答(第一个代码的问题是我试图删除项目的顺序)VbCrLf 和 VbNewLine 是相同的,并且在删除 VbLf 后尝试删除组合 vbCr+VbLf 将不起作用
回答by Mathieu Guindon
The premise is flawed:
前提是有缺陷的:
myString = "ABC" & vbCrLf & "DEF"
The string is made of "ABC", vbCrLf
, and "DEF".
字符串由“ABC”、“ vbCrLf
、”和“DEF”组成。
vbCrLf
is vbCr
and vbLf
, which on any Windows box is vbNewLine
.
vbCrLf
是vbCr
和vbLf
,在任何 Windows 机器上都是vbNewLine
。
When you do:
当你这样做时:
str1 = Replace(myString, vbLf, "")
You replace vbLf
and leave the vbCr
character in place.
您替换vbLf
并保留vbCr
角色。
str2 = Replace(str1, vbCrLf, "")
Then you replace vbCrLf
but vbLf
is already gone so vbCrLf
isn't in the string.
然后你替换vbCrLf
但vbLf
已经消失了所以vbCrLf
不在字符串中。
str3 = Replace(str2, vbNewLine, "")
Then you replace vbNewLine
which is basically doing the exact same thing as the previous instruction, and the result is a string that's been stripped of vbLf
but still contains vbCr
.
然后你替换vbNewLine
which 基本上和前面的指令做完全一样的事情,结果是一个被剥离vbLf
但仍然包含vbCr
.
This code works as expected:
此代码按预期工作:
Sub Test()
Dim foo As String
foo = "foo" & vbCrLf & "bar"
Debug.Print foo
foo = Replace(foo, vbNewLine, vbNullString)
Debug.Print foo
End Sub
As does this:
就像这样:
Sub Test()
Dim foo As String
foo = "foo" & vbNewLine & "bar"
Debug.Print foo
foo = Replace(foo, vbNewLine, vbNullString)
Debug.Print foo
End Sub
Or this:
或这个:
Sub Test()
Dim foo As String
foo = "foo" & vbNewLine & "bar"
Debug.Print foo
foo = Replace(foo, vbCrLf, vbNullString)
Debug.Print foo
End Sub
Or even this:
甚至这个:
Sub Test()
Dim foo As String
foo = "foo" & vbNewLine & "bar"
Debug.Print foo
foo = Replace(foo, vbCr, vbNullString)
foo = Replace(foo, vbLf, vbNullString)
Debug.Print foo
End Sub
Your second snippet works as intended, because you doremove both vbCr
(Chr(13)
) and vbLf
(Chr(10)
) characters. Simple as that.
您的第二个代码段按预期工作,因为您确实删除了vbCr
( Chr(13)
) 和vbLf
( Chr(10)
) 字符。就那么简单。