VBA Index Match 出现了类型不匹配错误。

huangapple go评论98阅读模式
英文:

VBA Index Match got a Type Mismatch Error

问题

我正在尝试使用Index&Match在2个工作表之间查找值。一个工作表定义为“Destination”,另一个工作表定义为“Origin”。

我的代码出现了类型不匹配错误。我已经测试了函数逻辑,这个公式在Excel中是有效的。我查看了其他类似的问题,但仍然无法解决。非常感谢任何建议。

Sub CalculateTracker()
    Dim LastRow As Long
    Dim Destination As Worksheet
    Set Destination = Worksheets("sheet1")
    Dim i As Integer
    Dim Origin As Worksheet
    Set Origin = Worksheets("sheet2")

    With Worksheets("sheet1")
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    For i = 2 To LastRow
        With Worksheets("sheet1")
            Cells(i, 28).Value = WorksheetFunction.IfError(WorksheetFunction.Index(Origin.Range("AZ:AZ"), Application.Match(Destination.Range("X" & i), Origin.Range("B:B"), 0)), 0)
        End With
    Next i
End Sub
英文:

I am trying to use index&Match to lookup values between 2 sheets. One sheet is defined as "Destination" and the other sheet is defined as "Origin".

My code got type mismatch error. I tested the function logic and the formula is working in the excel. I looked at other's similar issues here but still cannot solve it. It would be much appreciated for any suggestions.

Sub CalculateTracker()
    Dim LastRow As long
    Dim Destination As Worksheet
    Set Destination =Worksheets("sheet1")
    Dim i As Integer
    Dim Origin As Worksheet
    Set Origin = Worksheet("sheet2")

    With Worksheet("sheet1")
         LastRow=.Cells(.Rows.Count,"A").End(xlUp).Row
    End With

    For i=2 To LastRow
        With Worksheets("sheet1")
             Cells(i,28).Value=WorksheetFunction.ifError(WorksheetFuncion.Index(Origin.Range("AZ:AZ"),Application.Match(Destination.Range("X" & i),Origin.Range("B:B"),0)),0)
        End With

    Next i 

End Sub

答案1

得分: 3

Sub CalculateTracker()

    Const DST_FIRST_ROW As Long = 2
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' 包含此代码的工作簿
    
    Dim Origin As Worksheet: Set Origin = wb.Sheets("Sheet2")
    
    Dim Destination As Worksheet: Set Destination = wb.Sheets("Sheet1")
    
    Dim dlRow As Long
    
    With Destination
        dlRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    If dlRow < DST_FIRST_ROW Then Exit Sub ' 没有数据
    
    Dim r As Long
    
    For r = DST_FIRST_ROW To dlRow
        With Application
             Destination.Cells(r, "AB").Value _
                 = .IfError(.Index(Origin.Range("AZ:AZ"), _
                 .Match(Destination.Cells(r, "X"), Origin.Range("B:B"), 0)), 0)
        End With
    Next r

End Sub
英文:

Index/Match in VBA (Late Bound)

VBA Index Match 出现了类型不匹配错误。

A Quick Fix

<!-- language: lang-vb -->

Option Explicit

Sub CalculateTracker()
    
    Const DST_FIRST_ROW As Long = 2
    
    Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code
    
    Dim Origin As Worksheet: Set Origin = wb.Sheets(&quot;Sheet2&quot;)
    
    Dim Destination As Worksheet: Set Destination = wb.Sheets(&quot;Sheet1&quot;)
    
    Dim dlRow As Long
    
    With Destination
        dlRow = .Cells(.Rows.Count, &quot;A&quot;).End(xlUp).Row
    End With
    
    If dlRow &lt; DST_FIRST_ROW Then Exit Sub &#39; no data
    
    Dim r As Long
    
    For r = DST_FIRST_ROW To dlRow
        With Application
             Destination.Cells(r, &quot;AB&quot;).Value _
                 = .IfError(.Index(Origin.Range(&quot;AZ:AZ&quot;), _
                 .Match(Destination.Cells(r, &quot;X&quot;), Origin.Range(&quot;B:B&quot;), 0)), 0)
        End With
    Next r

End Sub

答案2

得分: 2

使用 IsError 来测试 Match 是否返回了一个错误。

Dim result As Variant
result = Application.Match(Destination.Range("X" & i), Origin.Range("B:B"), 0)

If Not IsError(result) Then
    Worksheets("sheet1").Cells(i, 28).Value = Origin.Range("AZ:AZ").Cells(result)
Else
    Worksheets("sheet1").Cells(i, 28).Value = 0
End If
英文:

Use IsError to test if Match returned an error.

Dim result As Variant
result = Application.Match(Destination.Range(&quot;X&quot; &amp; i),Origin.Range(&quot;B:B&quot;),0)

If Not IsError(result) Then
    Worksheets(&quot;sheet1&quot;).Cells(i,28).Value = Origin.Range(&quot;AZ:AZ&quot;).Cells(result)
Else
    Worksheets(&quot;sheet1&quot;).Cells(i,28).Value = 0
End If

huangapple
  • 本文由 发表于 2023年6月13日 00:49:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定