Solved

MS Excel VBA Match Function

Posted on 2011-09-08
12
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Industry Leaders: 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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
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.

707 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