Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 提取特定单词后的文本_Excel_Vba - Fatal编程技术网

Excel 提取特定单词后的文本

Excel 提取特定单词后的文本,excel,vba,Excel,Vba,我想从字符串中检索单词“for”后面的文本。在下面的示例中,我想检索“GLDSK8716” GLDSK8716-衍生工具合同-期货、远期、掉期和期权的完成通知 我尝试了这个公式,但它检索“for”之后的所有文本 试试看=MID(A1,FIND(,A1)+4,FIND(,A1,FIND(,A1)+4)-(FIND(,for,A1)+4) 假设您的文本位于单元格A1中,模式为-这是我的VBA版本: Option Explicit Public Function ExtractAfterWord(r

我想从字符串中检索单词“for”后面的文本。在下面的示例中,我想检索“GLDSK8716”

GLDSK8716-衍生工具合同-期货、远期、掉期和期权的完成通知

我尝试了这个公式,但它检索“for”之后的所有文本

试试看
=MID(A1,FIND(,A1)+4,FIND(,A1,FIND(,A1)+4)-(FIND(,for,A1)+4)


假设您的文本位于单元格A1中,模式为-

这是我的VBA版本:

Option Explicit

Public Function ExtractAfterWord(rngWord As Range, strWord As String) As String

    On Error GoTo ExtractAfterWord_Error

    Application.Volatile

    Dim lngStart        As Long
    Dim lngEnd          As Long

    lngStart = InStr(1, rngWord, strWord)
    If lngStart = 0 Then
        ExtractAfterWord = "Not Found"
        Exit Function
    End If
    lngEnd = InStr(lngStart + Len(strWord) + 1, rngWord, " ")

    If lngEnd = 0 Then lngEnd = Len(rngWord)

    ExtractAfterWord = Trim(Mid(rngWord, lngStart + Len(strWord), lngEnd - lngStart - 2))



    On Error GoTo 0
    Exit Function

ExtractAfterWord_Error:

    ExtractAfterWord = Err.Description

End Function
代码中的条件(
如果lngEnd=0
)是确保即使for在文本中的最后一个单词之前,公式仍然有效。还添加了“未找到”答案


我的解决方案是用VBA。下面的代码使用
RegEx
查找空格之间的所有单词。然后,它循环遍历所有单词,一旦找到“for”,它就会升起一个标志,指示下一个单词(
RegEx.Match
)就是您要查找的单词

代码

Option Explicit

Sub ExtractNumbers()

Dim Reg1 As Object
Dim RegMatches As Variant
Dim Match As Variant
Dim NextWord As Boolean

Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
    .Global = True
    .IgnoreCase = True
    .Pattern = "\w{1,50}" ' Match any set of characters up to length of 50, untill a space
End With

Set RegMatches = Reg1.Execute(Range("R2").Value)
NextWord = False ' <-- reset flag
If RegMatches.Count >= 1 Then        
    For Each Match In RegMatches
        If NextWord Then
            MsgBox "The word you are looking for is '" & Match & "'"
            Exit Sub
        End If
        If UCase(Match) Like "FOR" Then NextWord = True '<-- "for" found >> raise flag up
    Next Match
End If

End Sub
选项显式
子编号()
Dim Reg1作为对象
Dim RegMatches作为变量
作为变体的暗淡匹配
Dim NextWord为布尔型
Set Reg1=CreateObject(“VBScript.RegExp”)
使用Reg1
.Global=True
.IgnoreCase=True
.Pattern=“\w{1,50}”匹配长度不超过50的任何字符集,直到填入空格
以
设置RegMatches=Reg1.Execute(范围(“R2”).Value)
NextWord=False'=1然后
对于RegMatches中的每个匹配
如果是下一个
MsgBox“您要查找的单词是'&Match&'”
出口接头
如果结束
如果UCase(Match)像“FOR”,那么NextWord=True'>升起旗子
下一场比赛
如果结束
端接头
单元格R2中字符串的屏幕截图,并生成一个
MsgBox


您是否尝试过下面提供的任何答案?尝试给出反馈,并将您最终使用的答案标记为“答案”。我将错误标记为“子”或“查找”中未定义的函数(“您能粘贴完整错误吗?您的数据是否在单元格A1中?”?
Option Explicit

Sub ExtractNumbers()

Dim Reg1 As Object
Dim RegMatches As Variant
Dim Match As Variant
Dim NextWord As Boolean

Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
    .Global = True
    .IgnoreCase = True
    .Pattern = "\w{1,50}" ' Match any set of characters up to length of 50, untill a space
End With

Set RegMatches = Reg1.Execute(Range("R2").Value)
NextWord = False ' <-- reset flag
If RegMatches.Count >= 1 Then        
    For Each Match In RegMatches
        If NextWord Then
            MsgBox "The word you are looking for is '" & Match & "'"
            Exit Sub
        End If
        If UCase(Match) Like "FOR" Then NextWord = True '<-- "for" found >> raise flag up
    Next Match
End If

End Sub