Solved

Problems with Vlookup

Posted on 2013-11-10
4
223 Views
Last Modified: 2013-11-16
Hi Guys, I have to do a Vlookup on a Trade Id in Excel where sometimes there are multiple Ids in, for example "60500.28, 214875.28, 112930.28, 59295.28, 323976.28, 443444.28, 545488.28". The different trade Ids are because an Equity trade has been cancelled and replaced by a new one. Due to a system bug, sometimes the sequence of the trade Ids changes eg. from "60500.28, 214875.28, 112930.28, 59295.28, 323976.28, 443444.28, 545488.28" to "214875.28,112930.28, 443444.28, 323976.28,59295.28, 60500.28" so the Vlookup on the previous days Profit and Loss on that day's trade comes up as a "#NA". Is there a formula that can prevent this or a Macro which would enable the Vlookup to work?
0
Comment
Question by:Justincut
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Michael
ID: 39636865
Hi Justin,

can you post a workbook with a sample of your data, with an example of a situation where the vlookup works and one where the #N/A error occurs.
0
 
LVL 81

Expert Comment

by:byundt
ID: 39637053
One approach is to arrange your input substrings in ascending order. After you have done that to both the lookup values and the first column of the lookup table, the VLOOKUP formula will find a match.

Here is a macro that will reorder the values in a selected range of cells. Values that are blanks or numbers will be left alone. Only values separated by a comma and space will be resorted.
Sub NormalizeInputs()
Dim rg As Range
Dim s As String, sep As String, temp As String
Dim v As Variant, vData As Variant
Dim i As Long, j As Long, k As Long, n As Long, nSubstrings As Long
Dim bSorted As Boolean
sep = ", "    'Separator between elements
Set rg = Selection
Set rg = Intersect(rg, rg.Worksheet.UsedRange)   'If user selects an entire column, don't bother going beyond the used portion of the worksheet
vData = rg.Value
n = rg.Rows.Count
For i = 1 To n
    If vData(i, 1) <> "" And Not IsNumeric(vData(i, 1)) Then
        v = Split(vData(i, 1), sep)
        nSubstrings = UBound(v)
        If nSubstrings > 0 Then
            For j = 1 To nSubstrings
                bSorted = True
                For k = nSubstrings To 1 Step -1
                    If v(k) < v(k - 1) Then
                        bSorted = False
                        temp = v(k - 1)
                        v(k - 1) = v(k)
                        v(k) = temp
                    End If
                Next
                If bSorted = True Then Exit For
            Next
            s = ""
            For k = 0 To nSubstrings
                s = s & sep & v(k)
            Next
            vData(i, 1) = Mid(s, Len(sep) + 1)
            Set v = Nothing
        End If
    End If
Next
rg.Value = vData
End Sub

Open in new window

DummyRecQ28289149.xlsm
0
 

Author Comment

by:Justincut
ID: 39645406
Hi, when I try to run the "Normalise Inputs"  macro I get a type mismatch at this point:

"If vData(i, 1) <> "" And Not IsNumeric(vData(i, 1)) Then"

Can you try and run my attchment on yours and correct code?
0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39645606
Justincut,
You did not post a file in this question, so I took one from another of your questions and tested the code on it.

If you would like me to test the code on a certain file, please attach it to this question. Please also specify which version of Excel that you  are using.

I can reproduce an error on the statement you noted if only one cell was selected prior to running the macro. The macro expected you to select a column of cells. I added a workaround for a single cell being selected in the snippet below.
Sub NormalizeInputs()
Dim rg As Range
Dim s As String, sep As String, temp As String
Dim v As Variant, vData As Variant
Dim i As Long, j As Long, k As Long, n As Long, nSubstrings As Long
Dim bSorted As Boolean
sep = ", "    'Separator between elements
Set rg = Selection
Set rg = Intersect(rg, rg.Worksheet.UsedRange)   'If user selects an entire column, don't bother going beyond the used portion of the worksheet
If rg.Cells.Count > 1 Then
    vData = rg.Value
Else
    ReDim vData(1 To 1, 1 To 1)
    vData(1, 1) = rg.Value
End If
n = rg.Rows.Count
For i = 1 To n
    If vData(i, 1) <> "" And Not IsNumeric(vData(i, 1)) Then
        v = Split(vData(i, 1), sep)
        nSubstrings = UBound(v)
        If nSubstrings > 0 Then
            For j = 1 To nSubstrings
                bSorted = True
                For k = nSubstrings To 1 Step -1
                    If v(k) < v(k - 1) Then
                        bSorted = False
                        temp = v(k - 1)
                        v(k - 1) = v(k)
                        v(k) = temp
                    End If
                Next
                If bSorted = True Then Exit For
            Next
            s = ""
            For k = 0 To nSubstrings
                s = s & sep & v(k)
            Next
            vData(i, 1) = Mid(s, Len(sep) + 1)
            Set v = Nothing
        End If
    End If
Next
rg.Value = vData
End Sub

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

792 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