Solved

VBA Application.WorksheetFunction.Vlookup not working

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

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 49

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 49

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

809 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