Solved

# Problems with Vlookup

Posted on 2013-11-10
193 Views
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
Question by:Justincut
• 2

LVL 6

Expert Comment

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 80

Expert Comment

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
``````
DummyRecQ28289149.xlsm
0

Author Comment

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 80

Accepted Solution

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
``````
0

## Featured Post

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.