vba 从字符串中删除段落标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26633904/
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
Remove paragraph mark from string
提问by David Gard
I have a macro that finds all of the 'Heading 1' styles within my document and lists them in a ComboBox on a UserForm.
我有一个宏,可以在我的文档中找到所有“标题 1”样式,并将它们列在用户窗体上的组合框中。
My problem is that the Find routine I am using is also selecting the paragraph mark (?) after the text I wish to copy, and that is being displayed in the ComboBox.
我的问题是我正在使用的 Find 例程也在我希望复制的文本之后选择段落标记 ( ?),并且显示在 ComboBox 中。
How can I remove this from the string? I've tried useing replace()
, replacing vbCrLf
, vbCr
, vbLf
, vbNewLine
, ^p
, v
, Chr(244)
and Asc(244)
with ""
, but nothing has succeeeded. For example -
如何从字符串中删除它?我试过期运用replace()
,更换vbCrLf
,vbCr
,vbLf
,vbNewLine
,^p
,v
,Chr(244)
和Asc(244)
用""
,但一切都没有succeeeded。例如 -
sanitizedText = Replace(Selection.Text, "^v", "")
Can anyone please help with this problem? Thanks.
任何人都可以帮忙解决这个问题吗?谢谢。
Here is how my form looks -
这是我的表格的外观 -
回答by Miguel Febres
You should use ChrW$()
for unicode characters:
您应该使用ChrW$()
unicode 字符:
sanitizedText = Replace(Selection.Text, ChrW$(244), "")
Or, if the paragraph mark is always at the end maybe you can just remove the last character using
或者,如果段落标记总是在最后,也许您可以使用删除最后一个字符
myString = Left(myString, Len(myString) - 1)
回答by Draig Hodge
I used sanitizedText = Replace(Selection.Text, Chr(13), "")
successfully; 13
is the ASCII value for 'carriage return'.
我用sanitizedText = Replace(Selection.Text, Chr(13), "")
成功了;13
是“回车”的 ASCII 值。
回答by Christian Geiselmann
This tiny script replaces, in a piece of text selected in the document (i.e. marked using the cursor) hyphens that are at the beginning of a line. It replaces them by an improvised bullet point: (o)
这个小脚本替换了在文档中选择的一段文本(即使用光标标记)位于行首的连字符。它用一个即兴的要点代替它们:(o)
The script searches for occurances of "Paragraph mark followed by hyphen".
该脚本搜索“段落标记后跟连字符”的出现。
I had a similar problem as in the question above, as I was sure paragraph marks should be 'Chr(13) & Chr(10)', which is equal 'VbCrLF', which is equal 'Carriage Return, Line Feed'. However, 'Chr(13) & Chr(10)' were wrong. The naked Chr(13) did the job.
我遇到了与上述问题类似的问题,因为我确定段落标记应该是“Chr(13) & Chr(10)”,它等于“VbCrLF”,等于“回车,换行”。然而,'Chr(13) & Chr(10)' 是错误的。裸体 Chr(13) 完成了这项工作。
Sub MakeAppFormListPoints()
'Replace list hyphens by (o)
Dim myRange As Range
Set myRange = Selection.Range 'What has been marked with the cursor
Debug.Print myRange ' Just for monitoring what it does
myRange = replace(myRange, Chr(13) & "-", Chr(13) & "(o)")
Debug.Print myRange ' Just for monitoring what it does
End Sub
(I use this for adjusting text written in Word to the insanely restricted character set of the official application form for the European Union Erasmus+ programme to promote lifelong learning activities. Well, I learned something.)
(我用它来调整用 Word 编写的文本,使其适应欧盟 Erasmus+ 计划官方申请表的疯狂限制字符集,以促进终身学习活动。嗯,我学到了一些东西。)