vba 如何从包装的文本单元格中提取特定的文本行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9884144/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:39:36  来源:igfitidea点击:

How to extract a particular line of text from wrapped text cell?

excelvbalinecell

提问by Sarika.S

Is it possible to take only the first two lines from a wrapped text cellthrough VBA code?

是否可以通过 VBA 代码仅从包装的文本单元格中获取前两行

For example, a wrapped text cell has value as:

例如,换行文本单元格的值为:

aaaaaaa

bbbbbbb

ccccccc

ddddddd

So the cell contains 4 lines of wrapped text. I need to extract

因此该单元格包含 4 行换行文本。我需要提取

"aaaaaaaa

bbbbbbbb"

采纳答案by lori_m

With your text in A1, you can try this formula to extract the first two lines:

使用 A1 中的文本,您可以尝试使用此公式来提取前两行:

 =LEFT(A1,FIND(CHAR(10),A1,FIND(CHAR(10),A1)+2))

You can use =SUBSTITUTE(...,CHAR(10)," ")to replace line breaks with spaces.

您可以使用=SUBSTITUTE(...,CHAR(10)," ")空格替换换行符。

If you want to use VBA, you could use:

如果你想使用 VBA,你可以使用:

 split(cells(1,1),chr(10))(0) & " " & split(cells(1,1),chr(10))(1)

回答by mattboy

Well the VBA code for a line break like this is Chr10. So you'll need to use this to find the position of the second line break by using the Mid function, and get all the characters before that by using the Left function.

那么像这样的换行符的 VBA 代码是 Chr10。因此,您需要使用 Mid 函数使用它来查找第二个换行符的位置,并使用 Left 函数获取之前的所有字符。

If no one has given a clearer answer I will edit this question later when I have time to test.

如果没有人给出更明确的答案,我将在稍后有时间测试时编辑此问题。

EDIT

编辑

I noticed just now you said "wrapped text", not that they were line breaks like I assumed. Are the lines being created only by the column size, or do you have actual line breaks?

我刚才注意到你说“换行文本”,而不是像我想象的那样换行。线条是仅由列大小创建的,还是有实际的换行符?