Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copying data using key column and excluding #N/A

Posted on 2013-01-24
2
Medium Priority
?
234 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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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…

772 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