简体   繁体   English

Excel VBA错误13“类型不匹配”

[英]Excel VBA Error 13 “Type Mismatch”

I am relatively new to VBA, I've spent the weekend trying to work around the following error. 我是VBA的新手,我花了一个周末试图解决以下错误。 The error 13 is indicated for the line "If uuid(i, 1).value = (Polar Or ID107 Or Nut) Then" 对于行“如果uuid(i,1).value =(Polar或ID107或Nut)然后”,则指示错误13。

I am currently trying to check cells in a column to see if a specific string is contained in it. 我目前正在尝试检查列中的单元格,以查看其中是否包含特定的字符串。 If it is then copy the necessary cells in same row. 如果是,则在同一行中复制必要的单元格。 I know the error is due to the string/numerical assignment but I just cant seem to figure out or find a workaround. 我知道错误是由于字符串/数字赋值引起的,但我似乎无法弄清或找到解决方法。

My code is below I've also kept previous attempts in (but commented out) to try save some time. 我的代码在下面,我也保留了以前的尝试(但已注释掉),以节省一些时间。 I've also tried defining the strings as "Variants" to no avail. 我也曾尝试将字符串定义为“变量”,但无济于事。 no matter what I change ( while still trying to check for string match) this error is fixed on this line. 无论我进行了什么更改(尽管仍然尝试检查字符串是否匹配),此错误都已在此行修复。

Any and all help is very greatly appreciated Running Excel 2016, on Win10 非常感谢所有帮助,并在Win10上运行Excel 2016

Sub rssi_test()

    'Declare/ Define variables
    Dim output As Integer
    Dim i As Integer
    Dim Polar As String 'FF:31:F2:91:28:4A   Polar   H10
    Dim ID107 As String 'D3:AE:CB:BC:E6:C7   ID107   HR
    Dim Nut   As String 'FF:FF:30:00:E8:9D   nut
    Dim holder As String
    Dim point_change As String


    Dim class, uuid, rssi, value1, value2 As Range
    Dim class_results, uuid_results, rssi_results, value1_results, value2_results As Range
    Dim itemCount As Long

    'itemCount = Range("A:A").Rows.Count
    Set class = Range("B:B")
    Set uuid = Range("C:C")
    Set rssi = Range("D:D")
    Set value1 = Range("E:E")
    Set value2 = Range("F:F")

    Polar = "FF:31:F2:91:28:4A"
    ID107 = "D3:AE:CB:BC:E6:C7"
    Nut = "FF:FF:30:00:E8:9D"
    point_change = "moving"
    output = 1

    '''For i = 10000 To itemCount 'Starting value for "i" to full length of sheet
    For i = 1 To 10000

    '''If InStr(uuid.value, Polar, Nut) > 0 Then
    '''l = Application.WorksheetFunction.Match("TEST", Range("A1:A100"), 0)
    '''Worksheet.Find(uuid(i, 1)) = (Polar Or ID107 Or Nut)

    '''holder = uuid(i,1)
    '''If holder = (Polar Or ID107 Or Nut) Then


    If uuid(i, 1).value = (Polar Or ID107 Or Nut) Then

        'UUID Matches
        If value1(i, 1) < -1 Then
            Results output
            output = output + 1
        End If

    ElseIf uuid(i, 1) = point_change Then
        'Point change indicator
        Results output
        output = output + 1

    End If
    Next i
    '''i = i + 1

End Sub

Sub Results(x)
    Set class = Range("B:B")
    Set uuid = Range("C:C")
    Set rssi = Range("D:D")
    Set value1 = Range("E:E")
    Set value2 = Range("F:F")

    Set class_results = Range("L:L")
    Set uuid_results = Range("M:M")
    Set rssi_results = Range("N:N")
    Set value1_results = Range("O:O")
    Set value2_results = Range("P:P")

        class_results(x, 1) = class(x, 1)
        uuid_results(x, 1) = uuid(x, 1)
        rssi_results(x, 1) = rssi(x, 1)
        value1_results(i, 1) = value1(i, 1)
        value2_results(i, 1) = value2(i, 1)
End Sub

This should be written: 应该这样写:

 If uuid(i, 1).value = Polar Or uuid(i, 1).value = ID107 OR uuid(i, 1).value =Nut Then

OR and And are used between Booleans ( TRUE or FALSE ), not between Strings. ORAnd用于布尔值( TRUEFALSE )之间,而不用于字符串之间。 So you end up resolving to something like: 因此,您最终解决了以下问题:

If True OR False or False Then

Which will resolve to True and pass the IF . 它将解析为True并通过IF

you could use Select Case syntax: 您可以使用Select Case语法:

For i = 1 To 10000
    Select Case uuid(i, 1).Value
        Case Polar, ID107, Nut
            'UUID Matches
            If value1(i, 1) < -1 Then
                Results output
                output = output + 1
            End If

        Case point_change
            'Point change indicator
            Results output
            output = output + 1
    End Select
Next

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

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