Solved

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

Posted on 2013-06-17
2
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

628 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