Solved

Copying data using key column and excluding #N/A

Posted on 2013-01-24
2
221 Views
Last Modified: 2013-01-24
I want to copy data from one workbook to another using a key column that exist in both.

So far I've tried using VLOOKUP but I'm not sure it is the best way.

I have a button that runs a macro. First I select the cells I want the formula in:

Sub Copy_Formula()
    Selection.ClearContents
    Selection.FormulaR1C1 = _
        "=VLOOKUP(RC[-7],[Book2.xls]Sheet1!R5C3:R181C4,2,FALSE)"
End Sub

This works fine, BUT... I want the values in the cells, not the formula itself. And I want to clear the #N/A that I get in the cells if the formula has no match.

Any idea?
0
Comment
Question by:Mamarazzi
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 38813988
Using Excel 2003...



Sub Copy_Formula()
    With Selection
        .ClearContents 
        .FormulaR1C1 = _
            "=IF(ISNA(VLOOKUP(RC[-7],[Book2.xls]Sheet1!R5C3:R181C4,2,FALSE)),"""",VLOOKUP(RC[-7],[Book2.xls]Sheet1!R5C3:R181C4,2,FALSE))" 
        .Value = .Value
    End With
End Sub

Open in new window


In Excel 2007 and later:

Sub Copy_Formula()
    With Selection
        .ClearContents 
        .FormulaR1C1 = _
            "=IFERROR(VLOOKUP(RC[-7],[Book2.xls]Sheet1!R5C3:R181C4,2,FALSE),"""")" 
        .Value = .Value
    End With
End Sub

Open in new window

0
 

Author Closing Comment

by:Mamarazzi
ID: 38814112
Fabolous! Thanks
0

Featured Post

Technology Partners: 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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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