类型不匹配VBA无法找到

时间:2013-09-27 12:17:24

标签: excel vba excel-vba

我终于得到了一些正常工作的代码,但是当Module2函数完成后我得到了“类型不匹配”错误,我不明白为什么。

如果我单步执行它,它会逐步执行module2上的“End function”,然后我会收到Type Mismatch,但它会发送电子邮件。任何帮助都会很棒

此VBA代码分为3部分。

1 Sub

  Sub Workbook_open()

  Call Module1.GetData

  End Sub

2模块1

  Public EmailAddress As String
  Public CompanyNumber As String
  Public Name As String
  Public GroupComp As String

  Function GetData()

  Dim LastRow As String
  Dim rng As Range


  LastRow = Cells(Rows.Count, "K").End(xlUp).Row

  For Each rng In Range("K2:K" + LastRow)

       If Not rng.Value = vbNullString Then
           Select Case rng.Value
               Case 1
                  Case Is = "True"
                    Let EmailAddress = ActiveCell.Offset(0, -5).Value
                    Let CompanyNumber = ActiveCell.Offset(0, -9).Value
                    Let Name = ActiveCell.Offset(0, -8).Value
                    Let GroupComp = ActiveCell.Offset(0, -7).Value
                    Call Module2.Email(EmailAddress, CompanyNumber, Name, GroupsComp)
               Case 2
                  Case Is = "False"
           End Select

        End If
    Next
   End Function

3模块2

      Function Email()
     'MsgBox (EmailAddress)
     Set objMessage = CreateObject("CDO.Message")
     objMessage.Subject = "Stuffl " & (GroupComp)
     objMessage.From = "Department Name(Department@Email.com)"
     objMessage.Cc = "Department Name(Department@Email.com)"
     objMessage.To = (EmailAddress)
     MsgBox (EmailAddress)
     objMessage.TextBody = "TEST"

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "x.x.x.x"

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

      objMessage.Configuration.Fields.Update

      objMessage.Send

    End Function

2 个答案:

答案 0 :(得分:1)

Function Email()更改为

Function Email(emailaddress As String, companynumber As String, name As String, groupscomp As String)

你实际上并没有调用Mail函数,这就是我认为的问题

答案 1 :(得分:1)

“我不明白为什么我必须在邮件中再次指定变量”

每当您将值传递给Sub或Function时,您必须定义该Sub或Function,以便期望将值传递给它。所以这不起作用:

Sub Foo()
    Dim i as Integer
    i = 5
    Call Bar(i)
End Sub

Sub Bar()
    i = i + 2
End Sub

因为Bar()不希望传递任何东西。这将有效:

Sub Foo()
    Dim i as Integer
    i = 5
    Call Bar(i)
End Sub

Sub Bar(i as Integer)
    i = i + 2
End Sub

因为你现在告诉Bar希望传递一个整数。

希望有所帮助。