?
Solved

VBA Application.WorksheetFunction.Vlookup not working

Posted on 2014-01-30
6
Medium Priority
?
1,487 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
[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
  • 3
  • 3
6 Comments
 
LVL 52

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 52

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

 

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 52

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

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

770 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