【问题标题】:"Run-time error '13': Type Mismatch." when there is no email address“运行时错误‘13’:类型不匹配。”当没有电子邮件地址时
【发布时间】:2016-02-05 05:54:09
【问题描述】:

我无法解决通过 Excel 发送电子邮件时出现的错误。目前它被设置为当下拉列表在“N”列中标题为“打开”时,它会向“M”列中出现电子邮件地址的特定人员发送电子邮件(我在“J”中选择电子邮件的名称" 并在 "M" 中创建地址)。我认为问题可能在于它检查了我在“N”中“打开”的每一行,因此当没有电子邮件地址时,它会抛出“运行时错误'13':类型不匹配”。我目前有以下代码:

在表 1(问题表)中:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("N3")) Is Nothing Then
Select Case Range("N3")
    Case "Open": Macro1
End Select
End If
End Sub

我的模块是:

Option Explicit

子宏1() 将 OutApp 调暗为对象 将 OutMail 作为对象变暗 将单元格调暗为范围

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

On Error GoTo cleanup
For Each cell In Columns("M").Cells
    If cell.Value Like "?*@xyz.com" And _
       LCase(Cells(cell.Row, 14).Value) = "open" Then

        Set OutMail = OutApp.CreateItem(0)
        On Error Resume Next
        With OutMail
            .To = cell.Value
            .Subject = "Open Issue"
            .Body = "Dear " & Cells(cell.Row, "J").Value _
                    & vbNewLine & _
                    "Issue raised: " & Cells(cell.Row, "C").Value _
                    & vbNewLine & _
                    "Regards"
            'You can add files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display  'Or use Display
        End With
        On Error GoTo 0
        Set OutMail = Nothing
    End If
Next cell

cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub

基本上我想要它做的是能够只向地址发送电子邮件,在“打开”下拉列表中选择的行中,即在“N”列中选择“打开”,然后发送电子邮件仅到该行的“M”中的地址。我也需要它是可扩展的,这样我就可以向下走几行,只从那一行发送电子邮件。

【问题讨论】:

  • 哪一行报错了?
  • 如果 cell.Value 像 "?*@xyz.com" 和 _ LCase(Cells(cell.Row, "N").Value) = "open" 那么

标签: vba excel email


【解决方案1】:

我只解决了标题中的错误,我没有查看其余代码,也没有查看它是否符合您的要求。你的问题出在这一行

LCase(Cells(cell.Row, "N").Value) = "open" Then

Cells() 需要一个行号和一个列号,但您使用字符串(“N”)作为列号,这是类型不匹配,如果您想要列“N”然后使用数字 14 像这样

LCase(Cells(cell.Row, 14).Value) = "open" Then

【讨论】:

  • 错误依旧出现,是不是我的Sheet1还引用了N3?如果是这样,我该如何解决?
  • 我无法在此行重新创建错误。也许把你的文件贴在某个地方,我会看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
相关资源
最近更新 更多