Problems with Vlookup

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?
JustincutAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
byundtConnect With a Mentor Commented:
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
 
MichaelBusiness AnalystCommented:
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
 
byundtCommented:
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
 
JustincutAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.