2014-12-04 155 views
6

我试图使用Application.Match但是它返回一个type mismatch error:13错误。为什么?Application.Match给出类型不匹配

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 
+1

为什么不使用'Range.Find()'方法? – Chrismas007 2014-12-04 19:57:41

+0

显然,查找通常比匹配更慢:[fastexcel.wordpress.com](https://fastexcel.wordpress.com/2011/10/26/match-vs-find-vs-variant-array-vba-performance-枪战/) – Egalth 2017-06-15 18:13:29

回答

14

很可能没有找到匹配的结果。在这种情况下,Application.Match返回Excel错误代码,即值为Error 2042的变体/错误(这对应于在Excel中获取#N/A)。

这样的错误值不能隐式强制为一个字符串(这是MsgBox期望的),因此你得到的类型不匹配。

请注意,可以使用WorksheetFunction.Match调用相同的Match函数。唯一的区别是错误如何被处理:

  • 随着WorksheetFunction,错误使用On Error语法视为VBA错误,可捕获。

  • With Application,它们返回一个包含在Variant中的Excel错误代码。您可以使用IsError来查看返回的变量是否为错误类型变体。

+0

啊,那是我的问题!我一直在寻找“25%”,但当然工作表的内容是.25而不是字符串。谢谢 – As3adTintin 2014-12-04 19:59:03