在Excel中难以使用WorksheetFunction.Match()

时间:2014-07-19 02:20:14

标签: excel excel-vba vba

这是我为Excel编写的一些VBA代码。我正在尝试将Sheet1中的条目与Sheet2中的条目进行匹配。两张纸的结构如下:

DATE | ID |
----- ----
Date1 ID1
Date2 ID2...

在我的代码中,我循环遍历第一张表的行,并将每个特定行的值设置为MATCH()查询的一部分,希望在第二张表中找到相同的值。当我这样做时,我希望MATCH()返回它找到这些值的行索引,这样我就可以使用同一行从第一张表中输入更多信息。此查询使用多个条件,如valuesearchRange变量所示(我正在尝试通过连接方法使用多个条件,如this文章中所示)。

问题是,我一直得到一个WorksheetFunction.Match无法使用错误。当我使用一个单一标准(ID)时,该功能起作用。当我尝试使用多个时,它失败了,即使我按照之前链接文章中的说明进行操作。任何建议或想法来解决这个问题将不胜感激。

Sub runComparison(Sheet1 As String, Sheet2 As String)

    Dim rowCount As Variant, columnCount As Variant, information As Variant
    Dim counter As Integer
    Dim value As String, searchRange As String

    Sheets(Sheet2).Select

    'Array of the number of rows in both sheets
    rowCount = Array(Sheets(Sheet1).Cells(Rows.count, "A").End(xlUp).row, Sheets(Sheet2).Cells(Rows.count, "A").End(xlUp).row)

    'Array of the number of columns in both sheets
    columnCount = Array(Sheets(Sheet1).Cells(1, Columns.count).End(xlToLeft).Column, Sheets(Sheet2).Cells(1, Columns.count).End(xlToLeft).Column)

    'The range in which we will look for the date and the ID 
    searchRange = CStr(Range(Cells(2, 1), Cells(rowCount(1), 1)).Address & "&" & Range(Cells(2, 2), Cells(rowCount(1), 2)).Address)

    counter = 2
    Do Until counter = rowCount(0)

        'Sets the search term equal to the current cell in Sheet1
        value = Sheets(Sheet1).Cells(counter, 1) & "&" & Sheets(Sheet2).Cells(counter, 2)

        ' Attempts to set the cell in the 8th column in the same row in which the search term is found equal to a certain value from the search term's row
        Cells(WorksheetFunction.Match(value, searchRange, 0), 8) = Sheets(Sheet1).Cells(counter, columnCount(0)).value

        counter = counter + 1
    Loop

End Sub

编辑:这是一些示例输入

value = '7/14/2014&ESTUOUW1046465464'
searchRange = '$A2:$A298&$B2:B298'

1 个答案:

答案 0 :(得分:0)

<强>已更新

感谢您在评论中澄清。我删除了我的原始答案,因为它仅适用于常规的“匹配”功能,我看到了参考/示例,并了解了您现在要做的涉及数组公式的内容。

让我们使用Application.Evaluate来尝试这一点,这样可以避免将此公式放在单元格中。使用MS的示例数据,我做了这个似乎工作:

Sub test()

Dim value As String
Dim srcRange As String

value = "D2&E2"
srchRange = "$A$2:$A$5&$B$2:$B$5"

Debug.Print Application.Evaluate("=MATCH(" & value & "," & srchRange & ",0)")


End Sub

在您的代码中应用它,我认为如下所示。我想,你仍然希望Dim matchVal as Variant能够保持公式评估的结果。然后这样做:

Do Until counter = rowCount(0)

    'Sets the search term equal to the current cell in Sheet1
    value = Sheets(Sheet1).Cells(counter, 1) & "&" & Sheets(Sheet2).Cells(counter, 2)

    '## Assign the result of the  Match function to a variable
    matchVal = Application.Evaluate("=MATCH(" & value & "," & searchRange & ",0)")

    '## Check for errors, and handle as needed:
    If IsError(matchVal) Then
        'modify as needed, this highlight the cell with the non-matched value
        ' you might omit this line and simply ignore it, or you could
        ' display a MsgBox prompt, etc.
        Sheets(Sheet1).Cells(counter, columnCount(0)).Interior.ColorIndex = 6
    Else:
        Cells(matchVal, 8) = Sheets(Sheet1).Cells(counter, columnCount(0)).value
    End If

    counter = counter + 1

Loop