• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

MS Excel VBA Match Function

continuation of prior issue: I am trying to build an array to generate a report.  The data for the report is stored in various arrays, but when I use the MATCH function I am presented with "type-mismatch" yet the the common field in both arrays that I am searching on is variant\string in both.  The arrays in the code below are defined as follows: ReportArray(200,8) and ProdArray(200,2).  

My question is two-fold - Can Match be used on two multidirectional arrays? Why am I getting a "type-mismatch" when both arrays show the search as being variant\string in the "watch" area.

Below is the code generating the error message:
    For x = 1 To RptRows
        ID_Fnd = Application.Match(ReportArray(x, 1), ProdArray, 0)   ' (error is generated here)
        ReportArray(x, 7) = ProdArray(GPN_Fnd, 2)
    Next x
0
uad
Asked:
uad
  • 5
  • 3
  • 2
  • +2
1 Solution
 
zorvek (Kevin Jones)ConsultantCommented:
>Can Match be used on two multidirectional arrays?

If you mean multi-dimensional arrays then no.

>Why am I getting a "type-mismatch" when both arrays show the search as being variant\string in the "watch" area?

The first parameter must be a single variable. What is ReportArray(x, 1)?

The second parameter has to be a single dimension array.

Kevin
0
 
uadAuthor Commented:
I'm increasing the points.

If Match can't be used on two multi-dimensional arrays, what function can I use to locate the position of the matching record in the second array (ProdArray) based on matching of ID.  
ReportArray(x,1) is the single variable in the first array and position 1 is where the ID stored.

The ultimate goal is to build ReportArray with the data necessary to generate the report with data from different workbooks including modifying some of the data based on various criteria.
0
 
StephenJRCommented:
I'm not so sure. This will return the position of "fred" in the third column of Range A1:E5

MsgBox Application.Match("fred", Application.Index(Range("A1:E5"), 0, 3), 0)

I take no credit for this - somebody else's bright idea. Don't know if that's what you are after anyway.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
uadAuthor Commented:
StephenJR - I don't think that would work since I am working with arrays not a workbook range - that is, unless I am missing something in your suggestion
0
 
StephenJRCommented:
You could substitute an array for Range("A1:E5").
0
 
AgeOfEmpiresCommented:
Have you thought about using the Dictionary scripting object?  You can load the dictionary with the ID's as the keys and reference whatever data you want based on that key - no "MATCH"ing required.

VBA will easily loop through the members of a dictionary using the FOR EACH construct.

Much more flexible than arrays.

0
 
zorvek (Kevin Jones)ConsultantCommented:
This will work:

    ID_Fnd = Application.Match(ReportArray(x, 1), Application.Index(ProdArray, , 2), 0)

assuming you want to search the second "column" (ProdArray(1, 2), ProdArray(2, 2), etc.)

If you want to search the first column then:

    ID_Fnd = Application.Match(ReportArray(x, 1), Application.Index(ProdArray, , 1), 0)

Kevin
0
 
dlmilleCommented:
This is quite interesting.  Finding a match in one array against another - beats the heck out of looping throught the second array.

Any comments on efficiency from anyone?

PS - Not for points, please, but in your original code (and I know it will be modified with zorvek's excellent solution) note you have another error, I believe:

    For x = 1 To RptRows
        ID_Fnd = Application.Match(ReportArray(x, 1), ProdArray, 0)   ' (error is generated here)
        ReportArray(x, 7) = ProdArray(GPN_Fnd, 2)
    Next x
 
Should not GPN_Find be rather ID_Find ?  like this:

        ReportArray(x, 7) = ProdArray(ID_Fnd, 2)

Dave
0
 
uadAuthor Commented:
Dave - yes thank you. I did catch the GPN_Fnd v ID_Fnd.
0
 
uadAuthor Commented:
Kevin,

When I try your solution, it is now generating a "type mismatch".  

ReportArray(x,1) - watch is showing as variant/long
Application.Index(ProdArray, , 1) - watch is showing as variant/variant
0
 
uadAuthor Commented:
Although the total solution isn't resolved, I think the maximum points I can give for full resoltuion is not sufficient.  And, yes the solution did resolve the initial question and  created another issue which should be dealt with in another question.
0
 
zorvek (Kevin Jones)ConsultantCommented:
Note that an error occurs on this line:

    ID_Fnd = Application.Match(ReportArray(x, 1), Application.Index(ProdArray, , 2), 0)

because, when extracting a column versus a row, a two dimensional array is returned. The Transpose function solves this problem:

    ID_Fnd = Application.Match(ReportArray(x, 1), Application.Transpose(Application.Index(ProdArray, , 2)), 0)

Kevin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now