Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

vba - Application.Match gives type mismatch

I am trying to use Application.Match however it is returning a type mismatch error:13 error. Why?

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
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In all likelihood, no match is found. In such a case, Application.Match returns an Excel error code i.e. a Variant/Error whose value is Error 2042 (this corresponds to getting #N/A in Excel).

Such an Error value cannot be implicitly coerced to a String (which is what MsgBox expects) and thus you get the type mismatch.

Note that the same Match function can be called using WorksheetFunction.Match. The only difference is how errors are to be handled:

  • With WorksheetFunction, errors are treated as VBA errors, trappable using the On Error syntax.

  • With Application, they return an Excel error code wrapped in a Variant. You can use IsError to see if the returned variable is an Error type variant.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...