簡體   English   中英

VBA索引匹配運行時錯誤'13':類型不匹配

[英]VBA Index Match Run-time error'13': Type Mismatch

我收到運行時錯誤'13':在以下代碼上鍵入不匹配。 根據我對這個錯誤的了解,它似乎是由公式中包含兩種類型的變量引起的。 對我來說這是正確的,因為我的索引匹配函數將帶數字的單元格和帶文本的單元格合並起來以返回數字。 我試圖將數字設置為文本格式,但始終收到相同的錯誤。 看來這是問題所在嗎? 如果是這樣,是否有人知道這種/不同的方法來解決我在這里試圖做的事情? 謝謝!

Sub Lookup2()
    Dim cell As Range
    Dim lookUp1Sht As Worksheet
    Dim lookUp2Sht As Worksheet
    Dim lookUp2Rng As Range
    Dim val1 As Variant

    Set lookUp1Sht = ThisWorkbook.Worksheets("New") 
    Set lookUp2Sht = ThisWorkbook.Worksheets("input")
    Set lookUp2Rng = ThisWorkbook.Worksheets("comp").Range("A1:C136")

    For Each cell In Range("CaliforniaL") 
        With cell '
            Select Case True
                Case IsNumeric(.Value) 
                .Offset(0, 1).Value = CDbl(.Value)
                Case Else
                .Offset(0, 1).Value = (Application.WorksheetFunction.Index(lookUp2Sht.Range("K:K"), Application.WorksheetFunction.Match(cell.Value & cell.Offset(0, -3), lookUp2Sht.Range("A:A") & lookUp2Sht.Range("H:H"), 0)))
            End Select
        End With
    Next
End Sub

我猜問題出在這部分:

cell.Value & cell.Offset(0, -3)

cell.value返回單元格中的數據,而cell.Offset 返回范圍對象 由於Match函數需要lookup_value ,並且由於不能將值和范圍對象並置,因此將導致錯誤。 實際上,您根本無法連接范圍,因此match函數的第二個參數也將失敗:

lookUp2Sht.Range("A:A") & lookUp2Sht.Range("H:H") 

如果將這些公式放在工作表的一個單元格中(例如=match(A2 & d2, A:A & H:H, 0)您將發現它們將無法工作。

我從未見過在VBA代碼中使用工作表函數的價值。 我並不是說沒有價值,只是我從未見過需要。

我寫了一個函數來做“雙”匹配。 (Excel中可能有一個可用的,但是我懶得去找它。)使用它,並根據您的注釋中的一些澄清,可以將您的代碼重寫如下:

Sub Lookup2()
    Dim cell As Range
    Dim lookUp1Sht As Worksheet
    Dim lookUp2Sht As Worksheet
    Dim lookUp2Rng As Range
    Dim val1 As Variant

    Set lookUp1Sht = ThisWorkbook.Worksheets("New")
    Set lookUp2Sht = ThisWorkbook.Worksheets("input")
    Set lookUp2Rng = ThisWorkbook.Worksheets("comp").Range("A1:C136")

    For Each cell In Range("CaliforniaL")
        With cell
            If IsNumeric(.Value) Then
                .Offset(0, 1).Value = CDbl(.Value)
            Else
                val1 = DoubleMatch(lookUp1Sht.Cells(.Row, "A").Value, lookUp2Sht.Range("A:A"), _
                                   lookUp1Sht.Cells(.Row, "L").Value, lookUp2Sht.Range("H:H"))
                If IsError(val1) Then
                    .Offset(0, 1).Value = val1
                Else
                    .Offset(0, 1).Value = lookUp2Sht.Cells(val1, "K").Value
                End If
            End If
        End With
    Next
End Sub

Function DoubleMatch(Key1 As Variant, Range1 As Range, Key2 As Variant, Range2 As Range) As Variant
    'This function only performs an "Exact Match"
    If Range1.Rows.Count <> Range2.Rows.Count Then
        DoubleMatch = CVErr(xlErrRef)
        Exit Function
    End If
    If Range1.Columns.Count > 1 Or Range2.Columns.Count > 1 Then
        DoubleMatch = CVErr(xlErrRef)
        Exit Function
    End If

    Dim c As Range
    Dim r As Long
    Dim address1 As String
    With Range1
        Set c = .Find(What:=Key1, LookIn:=xlValues, LookAt:=xlWhole)
        If c Is Nothing Then
            DoubleMatch = CVErr(xlErrNA)
            Exit Function
        End If
        address1 = c.Address

        Do
            r = c.Row - Range1.Row + 1
            If Key2 = Range2(r, 1).Value Then
                DoubleMatch = r
                Exit Function
            End If
            Set c = .Find(What:=Key1, LookIn:=xlValues, LookAt:=xlWhole, After:=c)
            If c Is Nothing Then
                DoubleMatch = CVErr(xlErrNA)
                Exit Function
            End If
            If c.Address = address1 Then
                DoubleMatch = CVErr(xlErrNA)
                Exit Function
            End If
        Loop
    End With
    DoubleMatch = CVErr(xlErrNA)
End Function

暫無
暫無

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

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