Solved

Problems with Vlookup

Posted on 2013-11-10
4
238 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
[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
  • 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

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

729 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