Solved

Copying data using key column and excluding #N/A

Posted on 2013-01-24
2
216 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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…

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