简体   繁体   English

有条件的类型不匹配

[英]Type Mismatch in conditional

I am new at VBA, but i have been programming a lot of C#. 我是VBA的新手,但是我已经编写了很多C#。 My issue here is that i keep getting "Runtime error 13. Type Mismatch" error in my VBA function. 我的问题是,我的VBA函数中始终出现“运行时错误13。键入不匹配”错误。

I am extracting data from a Table in access and then trying to filter some of the data. 我正在从Access中的表中提取数据,然后尝试过滤一些数据。

My function looks like this: 我的函数如下所示:

Function FlowType(deliveryAdrID As String, deliverType As String, note As String) As String
    If (note = "*J") Then
        FlowType = "Weekend"
    ElseIf (deliveryAdrID = "62242" & deliverType = "H") Then
        FlowType = "AirGotland"
    ElseIf (deliveryAdrID <> "62242" & deliverType = "H") Then
        FlowType = "Air"
    Else
        FlowType = "Standard"
    End If
End Function

Why do I get this error? 为什么会出现此错误? The error occurs on this line: 该行发生错误:

ElseIf (deliveryAdrID = "62242" & deliverType = "H") Then

Your problem is because you use & , instead of And . 您的问题是因为您使用& ,而不是And The ampersand symbol is an operator to concatenate strings, whereas And is a Boolean operator. “&”符号是连接字符串的运算符,而“ And是布尔运算符。 So, change your comparisons like so: 因此,像这样更改您的比较:

ElseIf ((deliveryAdrID = "62242") And (deliverType = "H")) Then

With & , (assuming both conditions are satisified) the expression you are using will evaluate to 使用& ,(假设两个条件都满足),您使用的表达式将计算为

ElseIf ("TrueTrue") Then

which doesn't make sense since "TrueTrue" is not Boolean. 这没有意义,因为“ TrueTrue”不是布尔值。

You declare deliveryAdrID as Double, but you try to assign a String to it: 您将deliveryAdrID声明为Double,但是尝试为它分配一个String

deliveryAdrID = "62242"

That's a Type Mismatch, as VBA tries to tell you. VBA试图告诉您,这是类型不匹配。 ;) ;)

So, just use 因此,只需使用

deliveryAdrID = 62242

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM