excel vba - 如何从给定字符串中提取文本

时间:2017-09-29 08:27:30

标签: vba excel-vba excel

我在单元格中有以下文字。

我正在使用stackoverflow Stackoverflow是一个问答论坛 假设我还有一条线。

我已将其拆分为数组。

Arr[0] = I am using stackoverflow.   
Arr[1] = Stackoverflow is a question and answer forum.   
Arr[2] = Let’s say I have one more line.

如何从Arr[1]

中专门检索来自answer till forum.的字符串
  

输出应为 - answer forum

3 个答案:

答案 0 :(得分:1)

使用子字符串。

Dim substring As String = RIGHT(arr[1], 13)

其中13可以用子串的长度替换。确保长度不会导致索引超出范围异常。

答案 1 :(得分:1)

这可能不是最强大的答案,具体取决于您的用例。但是right()功能可以正常工作。

answerString = Right(Arr(1), 13)
'answer forum.

答案 2 :(得分:1)

我会假设您并不确切地知道Arr(1)中“"回答”字样的确切位置。发生了,那个词"论坛"不会在它之后直接发生,即变量Arr(1) 可能包含字符串"Stack Overflow is a question and answer site and is not a forum like so many other sites"。 (这实际上是Stack Overflow的更好描述!)

Dim temp As String
Dim result As String
arr(1) = "Stack Overflow is a question and answer site and is not a forum like so many other sites"

'Get everything after the first occurrence of "answer"
temp = Mid(arr(1), InStr(arr(1), "answer"))

'Get everything before the first occurrence of "forum"
result = Left(temp, InStr(temp, "forum") + 4)  ' + 4 because the InStr will point to the "f" of "forum"

'result will contain "answer site and is not a forum"