Solved

Excel - how to find value, replace with matching value from another sheet

Posted on 2013-06-17
2
307 Views
Last Modified: 2013-06-18
I need to write a maco that will look for values in an active selection, find match in my "replacement values" and replace with a value in same row. The selection may change each time I use it, but should always be one column.

For example in the attached - in sheet "my diet", if a value is equal to column A in sheet  "replacement values", replace with Column b in sheet "replacement values".
My replacement values worksheet will always be same reference, ie I can save it to hard drive and go back to it, I may add to it so I would like the macro to go to the sheet and repeat until end of data.
 
for example find in my diet where ever it says "apples" and replace with "fruit". Steak will remain as there is no match in replacement values.
Can someone post a simple macro that I can modify and use?
sample.xlsx
0
Comment
Question by:Mswetsky
2 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 39253630
This will replace the words in the selected range

Sub replacewords()
    Dim sws As Worksheet
    Dim tws As Worksheet
    Dim rw As Range
    Dim cel As Range
    Set sws = Sheets("Replacement values")
    Set tws = Sheets("My diet")
    For Each cel In Selection
        Set rw = sws.Range("A:A").Find(cel, , , xlWhole)
        If Not rw Is Nothing Then
            cel.Value = rw.Offset(, 1)
        End If
    Next cel
End Sub
0
 
LVL 1

Author Closing Comment

by:Mswetsky
ID: 39256128
Thank you for your prompt complete suggestion, It worked fine.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now