简体   繁体   English

VBA Excel错误13类型不匹配

[英]VBA Excel Error 13 Type mismatch

I have the next code that validate ID into 2 worksheets, it works okay but every time I run the macro I get the Mismatch error and I do not know what I am doing wrong or I do not know if I am missing something, I have checked all the answers before to come here but still nothing. 我有下一个验证ID到2个工作表中的代码,它可以正常工作,但是每次运行宏时,都会出现Mismatch错误,并且我不知道自己做错了什么,或者我不知道自己是否缺少一些东西,在来到这里之前检查了所有答案,但还是一无所获。

The error occurs at Set j = .Range("A:A").find(findValue) 错误发生在Set j = .Range("A:A").find(findValue)

This is my code: 这是我的代码:

Sub Save_comments()
Dim i As Integer
Dim j As Range

k = Sheets("List").Cells(Rows.Count, "P").End(xlUp).Row
For i = 1 To k
    findValue = Sheets("List").Cells(i, 16).Value
    With Sheets("Historical_Data")
        l = .Cells(Rows.Count, "A").End(xlUp).Row + 1
        Set j = .Range("A:A").find(findValue) '<-- error here
        If Not j Is Nothing Then
            If Sheets("List").Cells(i, 18).Value <> "" Then
                .Cells(j.Row, j.Column).Offset(0, 2).Value = Sheets("List").Cells(i, 18).Value
            End If
        Else
            .Cells(l, 1).Value = Sheets("List").Cells(i, 16).Value
            .Cells(l, 3).Value = Sheets("List").Cells(i, 18).Value
        End If
    End With
Next i
End Sub

Try to modify your code as follows: 尝试如下修改您的代码:

Sub Save_comments()
Dim i As Integer
Dim j As Range

k = Sheets("List").Cells(Rows.Count, "P").End(xlUp).Row
For i = 1 To k
    findValue = Sheets("List").Cells(i, 16).Value

    If Application.IsNA(findValue) = False Then

        With Sheets("Historical_Data")
            l = .Cells(Rows.Count, "A").End(xlUp).Row + 1
            Set j = .Range("A:A").Find(findValue) '<-- error here
            If Not j Is Nothing Then
                If Sheets("List").Cells(i, 18).Value <> "" Then
                    .Cells(j.Row, j.Column).Offset(0, 2).Value = Sheets("List").Cells(i, 18).Value
                End If
            Else
                .Cells(l, 1).Value = Sheets("List").Cells(i, 16).Value
                .Cells(l, 3).Value = Sheets("List").Cells(i, 18).Value
            End If
        End With

    End If
Next i
End Sub

The reason of your error is most probably N/A value assigned to findValue variable - this means that findValue variable has some error value. 错误的原因很可能是分配给findValue变量的N / A值-这意味着findValue变量具有一些错误值。 Here is the list of potential error numbers: 以下是潜在错误编号的列表:

在此处输入图片说明

This is how a minimal version of your code looks like: 这是代码的最低版本的样子:

Sub SaveComments()

    Dim i           As Long
    Dim j           As Range
    Dim findValue   As String

    For i = 1 To 20
        findValue = Sheets("List").Cells(i, 16).Value
        With Sheets("Historical_Data")
            Set j = .Range("A:A").Find(findValue)    '<-- error here
            Debug.Print j.Address
        End With
    Next i    
End Sub

If it does not work, simply try to see what is the findValue in the case when it breaks. 如果它不起作用,只需尝试看看在发生中断时的findValue是什么。

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

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