[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBA Application.WorksheetFunction.Vlookup not working

Posted on 2014-01-30
6
Medium Priority
?
1,575 Views
Last Modified: 2014-01-30
Hi All,

The following VBA code is not working for me:

Dim mylookup As Long
 Label335.Caption = ActiveCell.Offset(0, 5).Value
 myRng = Worksheets("Product Spec").Range("A2:BG10000")
 mylookup = Label335 
 Label582.Caption = Application.WorksheetFunction.VLookup(mylookup, myRng, 8, False)
End If

Open in new window


The code DOES work fine when Label335.caption = 1111116
but DOES NOT work when label335.caption = 111114X

I believe that this is something to do with the data type being set to long, but if I remove this then mylookup changes from 1111116 to "1111116" and the Application.WorksheetFunction.VLookup no longer works.

The label335.caption will always be seven characters long and either be numerical (e.g 1111116) or as text (e.g. 111114X)

 Many Thanks in advance
0
Comment
Question by:martinibbo
  • 3
  • 3
6 Comments
 
LVL 54

Expert Comment

by:Rgonzo1971
ID: 39820451
Hi,

pls try
Dim mylookup As Variant
 Label335.Caption = ActiveCell.Offset(0, 5).Value
set myRng = Worksheets("Product Spec").Range("A2:BG10000")
 mylookup = Label335 
 Label582.Caption = Application.WorksheetFunction.VLookup(mylookup, myRng, 8, False)
End If

Open in new window

Regards
0
 

Author Comment

by:martinibbo
ID: 39820470
Hi Rgonzo1971,

unfortunately your suggestion results in "run time error 1004, unable to get Vlookup Property of the WorksheetFunction class"

by changing the data type to Variant mylookup = "1111116", if this was set as long then the Ch34 (") would not appear and the code would execute, but as I say this wouldn't work for when mylookup ended in X.

Thanks
0
 
LVL 54

Expert Comment

by:Rgonzo1971
ID: 39820505
Hi,

Are you sure the value is present

I've got the same error when the myLookup value is not in the range

Regards
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:martinibbo
ID: 39820516
yes the value is definitely present.

if I use the following code then it works when label335 = 111114X but not when = 1111116
Label582.Caption = Application.WorksheetFunction.VLookup(Label335, myRng, 8, False)

Open in new window


if I use the code mentioned previously then it works when label335 = 1111116, but not when =111114X

???

Thanks
0
 
LVL 54

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 39820535
Hi,

the try to convert before the lookup

If IsNumeric(Label335) Then
    mylookup = CLng(Label335)
Else
    mylookup = Label335
End If

Open in new window

Regards
0
 

Author Comment

by:martinibbo
ID: 39820547
This works perfectly!

Thank you very much :)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

867 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