Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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_Outlook_Vba - Fatal编程技术网

Excel 如果电子邮件主题行以某些值开头,则应采取措施

Excel 如果电子邮件主题行以某些值开头,则应采取措施,excel,outlook,vba,Excel,Outlook,Vba,这行代码有什么问题?抛出运行时错误“13”,键入错误匹配 Dim mail As Outlook.MailItem If mail.Subject Like "VEH" & "*" Or "MAT" & "*" Then 尝试说出电子邮件主题是否以“VEH”或“MAT”开头,然后做些什么。您只需在此处使用VBALEFT函数即可: If UCASE(LEFT(TRIM(mail.Subject), 3)) = "VEH" OR UCASE(LEFT(TRIM(mail.Subj

这行代码有什么问题?抛出运行时错误“13”,键入错误匹配

Dim mail As Outlook.MailItem

If mail.Subject Like "VEH" & "*" Or "MAT" & "*" Then

尝试说出电子邮件主题是否以“VEH”或“MAT”开头,然后做些什么。

您只需在此处使用VBA
LEFT
函数即可:

If UCASE(LEFT(TRIM(mail.Subject), 3)) = "VEH" OR UCASE(LEFT(TRIM(mail.Subject), 3)) = "MAT" Then

'' DO something here

End If

If
语句的正确语法为:

If (mail.Subject Like "VEH*") Or (mail.Subject Like "MAT*") Then  

括号是可选的(
Like
运算符的优先级高于
),但更易于阅读

如果电子邮件来自不同的用户,那么我不建议在这种情况下使用类似于的
。如果它是机器生成的固定主题,那么它是有意义的

原因
Like/Left
区分大小写

示例

Sub Sample()
    Dim s As String

    s = "vehicle"

    If s Like "VEH*" Then
        MsgBox "Found a match"
    Else
        MsgBox "didn't find a match"
    End If
End Sub

备选方案

对于不区分大小写的搜索,例如
车辆
车辆
车辆
,请修改@PareshJ发布的内容

Sub Sample()
    Dim subj As String

    subj = "   Vehicle Number XYZ"

    If UCase(Left(Trim(subj), 3)) = "VEH" Then
        MsgBox "Found a match"
    Else
        MsgBox "didn't find a match"
    End If
End Sub
Trim
修剪前导空格和尾随空格,并
Ucase
将字符串转换为大写

编辑

如果您仍想像
一样使用
,则可能需要使用此选项

Option Compare Text '<~~ For case insensitive

Sub Sample()
    Dim subj As String

    subj = "   Vehicle Number XYZ"

    If Trim(subj) Like "VEH*" Then
        MsgBox "Found a match"
    Else
        MsgBox "didn't find a match"
    End If
End Sub

选项“比较文本”功能完美。非常感谢。我更喜欢这种方法。只要稍加修改:)是的,你是对的,西德哈特!需要删减主题并添加Ucase,以避免数据不一致。答案现在更新了。++现在有意义了:)@Downvoter:有什么评论吗?也许我能从你那里学到点什么?如果你只是不喜欢我的脸,那没关系。。。不用麻烦了。。。在这方面我帮不了你多少忙
Option Compare Text '<~~ For case insensitive

Sub Sample()
    Dim subj As String

    subj = "   Vehicle Number XYZ"

    If Trim(subj) Like "VEH*" Then
        MsgBox "Found a match"
    Else
        MsgBox "didn't find a match"
    End If
End Sub