簡體   English   中英

VBA Excel錯誤13類型不匹配

[英]VBA Excel Error 13 Type mismatch

我有下一個驗證ID到2個工作表中的代碼,它可以正常工作,但是每次運行宏時,都會出現Mismatch錯誤,並且我不知道自己做錯了什么,或者我不知道自己是否缺少一些東西,在來到這里之前檢查了所有答案,但還是一無所獲。

錯誤發生在Set j = .Range("A:A").find(findValue)

這是我的代碼:

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

嘗試如下修改您的代碼:

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

錯誤的原因很可能是分配給findValue變量的N / A值-這意味着findValue變量具有一些錯誤值。 以下是潛在錯誤編號的列表:

在此處輸入圖片說明

這是代碼的最低版本的樣子:

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

如果它不起作用,只需嘗試看看在發生中斷時的findValue是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM