Solved

MS Excel VBA Match Function

Posted on 2011-09-08
12
524 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

832 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