+ Reply to Thread
Results 1 to 4 of 4

VLookup for alphanumeric values resulting in error

  1. #1
    Registered User
    Join Date
    11-21-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    7

    VLookup for alphanumeric values resulting in error

    Hello all,

    I am currently trying to write a macro that will copy any data entries missing from 'Sheet1' in my excel workbook that are in 'Sheet2'. The missing entries are to be copied into a brand new sheet 'Sheet3'. As per my code below, the logic I used was to do a VLookup searching for the serial number in column D of Sheet 2, and compare them to those in Sheet 1. If the result of the VLookup was #N/A, then I would like the entire row of data associated with the serial number in Sheet 2 to be copied to Sheet 3.

    However, it seems that all of the VLookup results are giving an error, and the macro is copying all of the entries from Sheet 2 to Sheet 3. I have tried using the MsgBox function directily after the VLookup to show me the result, but the I get a Type Mismatch error (I am assuming this is because the VLookup result is a Variant). So then I tried the function MsgBox CStr(Missing), as can be seen below in the code, and this brings up the MsgBox but all it contains is 'Error 2015'.

    I hope I have been clear with my explanation and I appreciate any help!

    Below is my code:

    Sub FindMissing()

    Dim LastRow As Integer
    Dim Missing As Variant
    Dim RFAnum As Variant

    Worksheets("Sheet2").Select
    Range("D3").Select

    Do While IsEmpty(Selection.Value) = False
    Selection.Offset(1, 0).Select
    Loop
    LastRow = Selection.Row - 1

    For i = 4 To LastRow

    Worksheets("Sheet2").Select
    Range("D" & i).Select
    RFAnum = Selection
    ActiveCell.EntireRow.Copy

    Missing = Application.VLookup("RFAnum", "Sheet1!A3:AC250", 4, False)

    MsgBox CStr(Missing)

    If IsError(Missing) Then
    Sheets("Sheet3").Select
    Range("A1").End(xlDown).Offset(1, 0).Select
    ActiveCell.PasteSpecial


    End If

    Next i


    End Sub

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VLookup for alphanumeric values resulting in error

    excelMTL,

    Welcome to the forum! In the future, please wrap your code in code tags. See link in my sig for how.
    As for your question, give this a try:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    11-21-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: VLookup for alphanumeric values resulting in error

    Thank you tigeravatar for the quick response, it worked like a charm! I shall be sure to wrap my code in code tags in the future!!

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VLookup for alphanumeric values resulting in error

    You're very welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1