运行时错误91- Excel VBA

希望

我是VBA的新手,并且以下代码有问题,每次选择任何单元(G列中的一个除外)时,都会出现以下错误;

“运行时错误91-对象变量或块变量未设置”

Private Sub Worksheet_Change(ByVal Target As Range)
    Static mailSent As Boolean

    If Not mailSent And Range("G10:G250").Find("YES", MatchCase:=False).Count() > 0 Then
        SendMail
        mailSent = True
    End If
End Sub

Private Sub SendMail()
    With CreateObject("Outlook.Application").createitem(0)
         .To = "helpdesk171@***.com"
            .Subject = "*** Facility Manager Update"
            .Body = "Hi Property Services, " & vbNewLine & vbNewLine & "Update made by Facility Manager which requires your attention." & vbNewLine & vbNewLine & "Click Here <\\Internal_Gold Facility Inspection Action Tracker.xlsx>" & vbNewLine & vbNewLine & "Please amend the drop down in Column G accordingly (received/complete)" & vbNewLine & vbNewLine & " Kind regards "
            .Send
     End With
End Sub

当我单击debug时,它突出显示了这一行-“ If not mailSent And Range(“ G10:G250”)。Find(“ YES”,MatchCase:= False).Count()> 0然后”

有什么想法需要更改以防止此错误?

提前致谢 :)

778

我不确定您的布尔值,但是您需要测试“无”,以防万一Find找不到匹配项,例如

Private Sub Worksheet_Change(ByVal Target As Range)
    Static mailSent As Boolean
    Dim found As Range
    Set found = Range("G10:G250").Find("YES", MatchCase:=False)
    If found Is Nothing Then Exit Sub
    If Not mailSent And found.Count > 0 Then
        SendMail
        mailSent = True
    End If
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章