vba Application.Match 给出类型不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27302794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application.Match gives type mismatch
提问by As3adTintin
I am trying to use Application.Matchhowever it is returning a type mismatch error:13error. Why?
我正在尝试使用Application.Match但它返回一个type mismatch error:13错误。为什么?
Dim mySrs as Series
Dim ws as Worksheet
set ws Activesheet
For Each mySrs in ActiveChart.SeriesCollection
tempvar = mySrs.Name
y = Application.Match(tempvar, ws.Range("P37:P71"), 0)
MsgBox y
回答by Jean-Fran?ois Corbett
In all likelihood, no match is found. In such a case, Application.Matchreturns an Excel error code i.e. a Variant/Error whose value is Error 2042(this corresponds to getting #N/Ain Excel).
很可能没有找到匹配项。在这种情况下,Application.Match返回一个 Excel 错误代码,即一个变量/错误,其值为Error 2042(这对应于进入#N/AExcel)。
Such an Error value cannot be implicitly coerced to a String (which is what MsgBoxexpects) and thus you get the type mismatch.
这样的 Error 值不能被隐式强制为 String (这是MsgBox预期的),因此您会得到类型不匹配。
Note that the same Matchfunction can be called using WorksheetFunction.Match. The only difference is how errors are to be handled:
请注意,Match可以使用WorksheetFunction.Match. 唯一的区别是如何处理错误:
With
WorksheetFunction, errors are treated as VBA errors, trappable using theOn Errorsyntax.With
Application, they return an Excel error code wrapped in a Variant. You can useIsErrorto see if the returned variable is an Error type variant.
使用
WorksheetFunction,错误被视为 VBA 错误,可使用On Error语法捕获。使用
Application,它们会返回一个包含在 Variant 中的 Excel 错误代码。您可以使用IsError查看返回的变量是否为 Error 类型变体。

