Excel 2010 VBA匹配功能未找到匹配项

时间:2014-03-06 14:56:34

标签: excel vba match

在Excel 2010中,我的MATCH语句无效。该函数应返回由datasetId标识的数据集的行号(应该是唯一的),如果datasetId不存在,则返回0。无论datasetId是否存在于第一列中,该函数总是转到第二个分支并返回0

Function findDataset(dataWorksheet As Worksheet, datasetId As String) As Integer
    If Not VBA.IsError(Application.Match(datasetId, dataWorksheet.Columns(1), 0)) Then
        findDataset = Application.Match(datasetId, dataWorksheet.Columns(1), 0)
    Else
        findDataset = 0
    End If
End Function

1 个答案:

答案 0 :(得分:1)

您的功能没有任何根本性的错误:

Function findDataset(dataWorksheet As Worksheet, datasetId As String) As Integer
    If Not VBA.IsError(Application.Match(datasetId, dataWorksheet.Columns(1), 0)) Then
        findDataset = Application.Match(datasetId, dataWorksheet.Columns(1), 0)
    Else
        findDataset = 0
    End If
End Function

Sub MAIN()
    Dim ws As Worksheet
    Dim id As String, n As Integer
    Set ws = Sheets("Sheet1")
    id = "XX"
    n = findDataset(ws, id)
    MsgBox n
End Sub

为我工作..............也许你的比赛并不完美。