【问题标题】:Extract parts from Outlook email subject and then find it in an Excel spreadsheet从 Outlook 电子邮件主题中提取部分,然后在 Excel 电子表格中找到它
【发布时间】:2018-02-15 15:47:41
【问题描述】:

我的电子邮件主题如下所示: RE: Blah blah - Blah blah, 1234-56, Blah blah blah

我想知道如何从电子邮件主题中提取1234-56

我试过了:

If myemail.Subject Like "****-**" Then

但这只会给我真/假,我想要实际数据,1234-56

此外,我希望使用电子表格上的查找功能来查找数据所在的位置。

例如,由于电子邮件主题包含1234-56,我将继续我的电子表格并使用查找功能查找1234-56

我试过了:

Cells.Find( _
    What := myemail.Subject, _
    After := ActiveCell, _
    LookIn := xlValues, _
    LookAt := xlPart, _
    SearchOrder := xlByRows, _
    SearchDirection := xlNext, _
    MatchCase := False, _
    SearchFormat := False).Activate

但我认为这不是正确的语法。
提前感谢您的帮助!

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    您可以为此任务使用正则表达式。这是一个示例,它显示了如何在随机字符串中找到像 ####-##(四位数字、破折号、两位数字)这样的模式。

    Option Explicit
    
    Public Sub RegEx()
        'this is your subject
        Const Subject As String = "RE: Blah blah - Blah blah, 1234-56, Blah blah blah"
    
        Dim objRegEx As Object
        Set objRegEx = CreateObject("vbscript.regexp")
    
        With objRegEx
            .Global = True
            .IgnoreCase = True
            .MultiLine = False
            .Pattern = "[0-9]{4}-[0-9]{2}" 'this is the pattern to find ####-## where # is a number from 0-9
    
            Dim objMatches As Object
            Set objMatches = .Execute(Subject)
        End With
    
        'print all matches in the intermediate window
        Dim objMatch As Object
        For Each objMatch In objMatches
            Debug.Print objMatch
        Next
    End Sub
    

    要测试和构建模式,您可以轻松使用regex101.com

    【讨论】:

    • 感谢您的帮助!并感谢分享正则表达式功能,一定会记住它以备将来使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2015-03-07
    • 1970-01-01
    • 2022-11-24
    • 2015-07-09
    • 2011-12-17
    相关资源
    最近更新 更多