Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Excel VBA Match Function

Posted on 2011-09-08
12
Medium Priority
?
544 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 1500 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 42

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

782 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