?
Solved

XY Look up function for Excel range.  Return array of results to VBA subroutine

Posted on 2012-04-11
9
Medium Priority
?
492 Views
Last Modified: 2012-05-10
Any direction or help would be appreciated.  I have attached a file for you to look at.  What I need is basically a XY table lookup in VBA BUT there can be more than 1 result.  And I need it to ignore blank values and blank lines.


Please refer
For example, if I pass Joe and Period 4 to the funciton.  I would expect an array back with 2 elements of France, .75 and Italy, 1

It also needs to ignore blank lines and numbers.
XYlookup.xlsx
0
Comment
Question by:drhamel69
[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
  • 4
  • 3
9 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 37833255
How are you intending to use this array?
0
 
LVL 2

Author Comment

by:drhamel69
ID: 37833353
I have even more coding after this to loop through other things.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 37833371
OK, didn't read your question title properly. I would have a procedure with arguments including the range and then no need to bother about whether there are blank lines.
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!

 
LVL 2

Author Comment

by:drhamel69
ID: 37833391
that would work too as long as I am passed an array back
0
 
LVL 24

Accepted Solution

by:
StephenJR earned 2000 total points
ID: 37833436
Here is one approach. I'm assuming you want a 2x2 array returned?
Function XYLookup(rTable As Range, vName, vPeriod) As Variant

Dim vOut(), i As Long, c As Long, rFind As Range, sAddr As String
 
c = Application.Match(vPeriod, rTable.Rows(1), 0)

With rTable.Columns(2)
    Set rFind = .Find(What:=vName, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    If Not rFind Is Nothing Then
        sAddr = rFind.Address
        Do
            i = i + 1
            ReDim Preserve vOut(1 To 2, 1 To i)
            vOut(1, i) = rFind.Offset(, -1)
            vOut(2, i) = rTable(rFind.Row - rTable(1).Row + 1, c)
            Set rFind = .FindNext(rFind)
        Loop While rFind.Address <> sAddr
    End If
End With

XYLookup = Application.Transpose(vOut)

End Function

Sub x()

Dim v, i As Long

v = XYLookup(Range("A2:H11"), "Joe", "Period 4")

For i = LBound(v, 1) To UBound(v, 1)
    MsgBox v(i, 1) & "," & v(i, 2)
Next i

End Sub

Open in new window

0
 
LVL 2

Author Comment

by:drhamel69
ID: 37833528
WOrks fine on the example I gave but when I imported into my actual project i am getting a type mismatch error on c = Application.Match(vPeriod, rTable.Rows(1), 0)

Any Ideas?
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 37833941
I didn't put in any error trapping. It means the period value isn't found in the first row of your range.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 37951046
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

765 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