Solved

MS Excel VBA Match Function

Posted on 2011-09-08
12
520 Views
Last Modified: 2012-05-12
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
Comment
Question by:uad
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 36506210
>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
 

Author Comment

by:uad
ID: 36506421
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
 
LVL 24

Expert Comment

by:StephenJR
ID: 36506478
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
 

Author Comment

by:uad
ID: 36506583
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
 
LVL 24

Expert Comment

by:StephenJR
ID: 36506715
You could substitute an array for Range("A1:E5").
0
 
LVL 4

Expert Comment

by:AgeOfEmpires
ID: 36506736
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 36507110
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
 
LVL 41

Expert Comment

by:dlmille
ID: 36508200
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
 

Author Comment

by:uad
ID: 36510606
Dave - yes thank you. I did catch the GPN_Fnd v ID_Fnd.
0
 

Author Comment

by:uad
ID: 36523013
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
 

Author Closing Comment

by:uad
ID: 36536277
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
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 37491767
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now