Solved

VBA Application.WorksheetFunction.Vlookup not working

Posted on 2014-01-30
6
1,326 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 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
Comment Utility
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 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:martinibbo
Comment Utility
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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
This works perfectly!

Thank you very much :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Outlook Free & Paid Tools
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now