我正在尝试在两个工作簿之间进行匹配搜索,以查看在 Wbook1 中输入的名称是否在 Wbook2 的 A 列中。例如...我在 workbook1 的单元格 D4 中有“name1”...然后我希望宏搜索 workbook2 的 A 列以查找“name1”的位置。我不担心 workbook2 上不存在的名称,因为它应该始终存在。
使用 Excel 2007,代码为:
Sub ViewData()
Dim xlo As New Excel.Application
Dim xlw As New Excel.Workbook
Dim xlz As String
Dim result As Double
Dim SalesExec As String
SalesExec = Range("d4").Value 'D4 contains the name from workbook1 I want to search for
xlz = Range("y1").value 'This cell contains the file path for workbook 2
Set xlw = xlo.Workbooks.Open(xlz) 'Path is correct as the desired workbook does open
result = Application.WorksheetFunction.Match(SalesExec, xlo.Worksheets("Data").Range("A:A"), 0) 'Data is the sheet in workbook2 containing the list of names
Range("Q14").value = result
xlw.Save
xlw.Close
Set xlo = Nothing
Set xlw = Nothing
End Sub
如果我删除 .WorksheetFunction,我会收到“对象或应用程序定义错误”。正如代码所示,我收到“无法获取工作表函数类的匹配属性”错误,我不知道为什么。
任何帮助将非常感激。谢谢!