我的电子邮件主题如下所示:
回复: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
但我认为这不是正确的语法。
预先感谢您的帮助!
最佳答案
您可以使用正则表达式来完成此任务。下面的示例展示了如何在随机字符串中找到类似于 ####-##
(四位数字、破折号、两位数字)的模式。
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
关于excel - 从 Outlook 电子邮件主题中提取部分内容,然后在 Excel 电子表格中找到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48811134/