VBA语法匹配方法

y = Application.WorksheetFunction.Match(51187, Sheets("New Master Data 6.1").Range(Cells(1, 1), Cells((Rows.Count), 1)), 0) 

我无法find我的语法有什么问题。 请帮助 :)

如果“New Master Data 6.1”不是活动工作表,则代码将会出错,因为在常规代码模块中,任何没有合格工作表的Cells()实例都将引用活动工作表。

我会用像这样的东西:

 With Sheets("New Master Data 6.1") y = Application.Match(51187, .Columns(1), 0) End with If Not IsError(y) Then 'do something with y Else 'value was not found End If 

请注意,删除WorksheetFunction允许您testing返回值,而不是在不匹配时触发运行时错误。