在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
答案 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
为我工作..............也许你的比赛并不完美。