Solved

VBA Application.WorksheetFunction.Vlookup not working

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

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 50

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 50

Accepted Solution

by:
Rgonzo1971 earned 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to extract unique list from two columns 9 35
Tricky Shapes formula 3 18
Tricky shapes formula part 2 4 21
Tricky shapes formula part 3 4 33
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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