首页 > 解决方案 > 如何通过VBA检测Excel中的换行符

问题描述

我试图通过迭代它们并针对每个单元格中的文本运行 InStr 函数来在各个单元格中找到回车符\换行符。以下查询在大多数单元格中正确找到换行符,但在其中一个单元格中失败。

这些单元格中的换行符对所有单元格都添加了相同的内容,即按 Alt+Enter。

以下是我正在使用的功能:

InStr(startlocation, text, vbLf)

我也尝试了以下所有方法,但无济于事:

InStr(startlocation, text, Chr(10)) 'this seems to be identical in results to using vbLf
InStr(startlocation, text, Chr(13)) 'No results with this
InStr(startlocation, text, ALT + 10) 'I see this returning some results sometimes, not exactly sure for which character though
InStr(startlocation, text, vbCrLf) 'No results with this

有没有其他方法来表示换行符,所以我将它添加到我的查询中?

标签: excelvba

解决方案


Alt用+创建的单元格中的换行符EntervbLf.

因此,以下应该工作。

InStr(startlocation, text, vbLf)

如果它不起作用,则意味着您做错了其他事情。

如果您在单元格 A1 中有以下数据(Alt+Enter之后12

在此处输入图像描述

然后InStr(1, Range("A1"), vbLf)返回2


例如,您可以使用……</p>

Dim ArrLines As Variant
ArrLines = Split(Range("A1"), vbLf)

将这些行拆分成一个数组,例如……</p>

  • ArrLines(0)1
  • ArrLines(1)2
  • ArrLines(2)3

推荐阅读