• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Vlookup Function problem

Hi Guys, because of the Vlookup limit of 256 characters, I have had to create a Function which creates my own Vlookup, but my spreadsheet is very slow as its covering the whole column.How do I just adapt it to the first 1000 rows?  Code is below.



Function MyVlookup(Lval As Range, c As Range, oset As Long) As Variant
Dim cl As Range
For Each cl In c.Columns(1).Cells
    If UCase(Lval) = UCase(cl) Then
        MyVlookup = cl.Offset(, oset - 1)
        Exit Function
    End If
    Next
End Function
0
Justincut
Asked:
Justincut
  • 4
  • 3
  • 2
  • +4
1 Solution
 
QuinnDexCommented:
Function MyVlookup(Lval As Range, c As Range, oset As Long) As Variant
Dim cl As Range
Dim count =1

For Each cl In c.Columns(1).Cells
if count < 1000
    If UCase(Lval) = UCase(cl) Then
        MyVlookup = cl.Offset(, oset - 1)
        count = count+1
        Exit Function
    End If
else
       Exit Function
end if
    Next
End Function
0
 
Rgonzo1971Commented:
Hi,

pls try

it reduces the rows to the last row from the bottom
Function MyVlookup(Lval As Range, c As Range, oset As Long) As Variant
Dim cl As Range
Set mycolrange = Range(Cells(1, c.Columns(1)), Cells(Rows.Count, c.Columns(1)).End(xlUp))
For Each cl In mycolrange
    If UCase(Lval) = UCase(cl) Then
        MyVlookup = cl.Offset(, oset - 1)
        Exit Function
    End If
    Next
End Function

Open in new window


Personnaly I would have Change it to

Function MyVlookup(Lval As Range, c As Range, oset As Long) As Variant
Dim cl As Range
Set mycolrange = Range(Cells(1, c.Column), Cells(Rows.Count, c.Column).End(xlUp))
For Each cl In mycolrange
    If UCase(Lval) = UCase(cl) Then
        MyVlookup = cl.Offset(, oset - 1)
        Exit Function
    End If
    Next
End Function

Open in new window

to use the column of the range

Regards
0
 
byundtCommented:
When you test the code suggested by Rgonzo1971, make sure that the worksheet containing the lookup table is active. You will get an unexpected result if it is not--lookup will be performed on cells with same address only in the active worksheet.

I would also suggest changing Lval from a Range to a Variant. That way, you can test your function with numbers or text constants as well as range references.

Finally, you may want to try using VLOOKUP before looping through all the cells. If it returns a value, then you don't need to do anything else.

I tweaked Rgonzo1971's code to cater to the above issues:
Function MyVlookup(Lval As Variant, c As Range, oset As Long) As Variant
Dim cl As Range, MyColRange As Range
Dim v As Variant
On Error Resume Next
v = Application.VLookup(Lval, c, oset, False)
On Error GoTo 0

If IsError(v) Then
    With c.Worksheet
        Set MyColRange = Range(c.Cells(1, 1), .Cells(.Rows.Count, c.Column).End(xlUp))
    End With
    For Each cl In MyColRange
        If UCase(Lval) = UCase(cl) Then
            v = cl.Offset(, oset - 1)
            Exit For
        End If
    Next
End If
MyVlookup = v
End Function

Open in new window

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

 
zalazarCommented:
Instead of using Vlookup or using some code as suggested you might want to have a look at using a formula with Index in combination with Match,

E.g. Looking up D1 in B1:B10 and returning a result from A1:A10
  (note that ; is used as separator this might be different in your situation)
=INDEX(A1:A10;MATCH(TRUE;INDEX(B1:B10=D1;0);0))

Open in new window

As Index doesn't have the 256 chars limitation but Match has you have to use Index twice in the formula. The other advantage of using Index and Match is that you don't have to sort the range where you lookup the value.
0
 
byundtCommented:
zalazar,
That's a neat workaround! And it doesn't even need to be array-entered.

You can eliminate one of the INDEX functions if you array-enter the following:
=INDEX(A1:A10,MATCH(TRUE,B1:B10=D1,0))

I did get a noticeable recalc delay when I replaced A1:A10 and B1:B10 with entire columns, so Justincut would be well advised to restrict the formula to the first 1000 rows or so.

Brad
0
 
JustincutAuthor Commented:
On this attachment then, I want the "AD" column on the "Rec" tab to lookup up Column B on the "Prevday" tab and take the values of the "AD" column on the "Prevday" tab. How would I use Index, Match in this scenario?
DummyRec6.xlsx
0
 
barry houdiniCommented:
Try this formula in AD5 copied down

=INDEX('Prev Day'!AD$5:AD$1000,MATCH(TRUE,INDEX('Prev Day'!B$5:B$1000=B5,0),0))

that uses zalazar's suggestion above.

You seem to be restricting column B to 255 characters anway, using LEFT function, so a traditional VLOOKUP would give you the same result in those circumstances......but if you remove the LEFT function from column B then some of the results can only be achieved by this approach (e.g. rows 602 and 603)

regards, barry
0
 
JustincutAuthor Commented:
Ok, I am still getting NA's in Column AD. see attachment. Any ideas why?
DummyRec6.xlsx
0
 
barry houdiniCommented:
I'm assuming that for some of the entries there's just no match. The cell contents would have to match exactly (except for case), so, for instance, B5 doesn't exactly match 'Prev day'!B5, are you expecting a match there or somewhere else?

regards, barry
0
 
JustincutAuthor Commented:
Hi Guys, can you do an INDEX MATCH to another file? We basically roll a daily Rec every day by date and then do a Vlookup on the Previous Day
0
 
aikimarkCommented:
Two thoughts:
1. Since you have a static value to compare, you should do the UCase() outside any looping structures.
2. Since you need to do a text (non-binary) comparison, you should do a performance comparison of the UCase() against StrComp() and InStr() functions, which have a comparison type parameter.
0
 
JustincutAuthor Commented:
Can you say that in a simpler way? I am not a very good programmer.
0
 
byundtCommented:
If you rename the previous day file every day to a standard file name (like Previous Day.xlsx) and open it before opening the file with INDEX & MATCH, you will not need to update your formulas. They will automatically draw data from the previous day file as required.
0
 
zalazarCommented:
My share to solve this issue was very small but anyway...
@Brad, thanks for appreciating the workaround and eliminating one index lookup which makes it much better.
0

Featured Post

Independent Software Vendors: 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!

  • 4
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now