0

我试图在一张表(sheet2)上循环浏览客户的姓名,在 sheet1 的 J 列中获取相应的值,然后粘贴到原始表上的客户旁边。

这是我的代码:

For i = 0 To 9

Dim rowi As Long

rowi = Application.WorksheetFunction.Match((Worksheets("Sheet2").Cells(5 + i, 4)), Worksheets("Sheet1").Range("B:B"), 0)
Crystali = Cells(rowi, 10)
Sheets("Sheet2").Activate
Worksheets("Sheet2").Cells(5 + i, 7) = Crystali
Next i

有人可以帮我解决吗?我不断收到错误“无法获取 Worksheetfunction 类的匹配属性”

提前致谢。

4

1 回答 1

0

这里的简短示例如何使用'Match()' 函数而不像 Tim 建议的那样使用 'worksheet-function' 部分。调用 Match 函数后,只需使用'IsError()'检查结果。

Option Explicit

Public Sub test()
    Dim row_index As Long
    Dim match_result As Variant
    Dim lookup_value As Variant
    Dim lookup_array As Variant

    Set lookup_array = Range("Sheet1!B:B")

    Const FIRST_ROW As Byte = 5
    Const LAST_ROW As Byte = 9

    For row_index = FIRST_ROW To LAST_ROW

        Set lookup_value = Range("Sheet2!D" & row_index)
        match_result = Application.Match(lookup_value, lookup_array, 0)

        If Not IsError(match_result) Then
            ' copy data only if Match function found something
            Range("Sheet2!G" & row_index) = Range("Sheet1!J" & match_result)
        End If

    Next row_index
End Sub
于 2013-11-05T20:32:01.717 回答