首页 > 解决方案 > excel 2010 上的运行时错误 13 但适用于 excel 2016

问题描述

Private Sub CommandButton64_Click() 
Dim cell As Range
Dim strto As String
For Each cell In ThisWorkbook.Sheets("Sheet2").Range("C3:L197")
If cell.Value Like "?*@?*.?*" Then
strto = strto & cell.Value & ";"
End If
Next cell
If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)

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

On Error GoTo cleanup
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.BCC = strto
.Subject = "Enter subject here"
.Body = "" ' EMPTY FOR NOW
'USE THIS FOR ENTERING NAMES OF RECIPIENTS IN BODY TEXT "here"
'"Dear" & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"Enter body text " & _
"here"
'You can add files also like this
'.Attachments.Add ("C:\test.txt")
'.Send 'Or use Display
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub

该代码旨在将单元格中的电子邮件作为公式,并在单击命令按钮时将它们作为地址输出到密件抄送框中。

当我单击按钮时,此功能适用于 Excel 2016,但当我使用 Excel 2010 将文件转发给同事时不起作用,而是收到错误

运行时错误“13”:类型不匹配

突出显示文本行 'If cell.Value Like "? @? .?*" Then'

任何人都可以帮助我吗?

谢谢

标签: vbaexcelruntime-errortype-mismatch

解决方案


我刚刚解决了与该错误类似的问题。2010 版本很可能存在工作表计算错误。

如果您无法清除表格中的错误,请使用@Vityata 的建议

Not IsError

因此,对于您的代码,使循环更像:

For Each cell In ThisWorkbook.Sheets("Sheet2").Range("C3:L197")
    If Not IsError(cell) Then
        If cell.Value Like "?*@?*.?*" Then strto = strto & cell.Value & ";"
    End If
Next cell

推荐阅读