Search and Replace with Ranges

ConfusedinCanada

New Member
Joined
Jul 11, 2019
Messages
1
Hi there,
So I've been given a task to find and replace items in a spreadsheet with a range. Now I've had 0 experience in VBA before this, so I'm a bit confused. And could use some help.
I will put it as simply as I can, because my brain gets confused when I go into the details of the task.


We'll say we have a spreadsheet with the workbook "Fruit". This workbook has two columns.
Column A is "Type", and Column B is "Location Picked".
We also have two ranges, named range1 and range2
range1 has ("orange", "apple", "pear"), and range2 ("Spain", "England", "France")
So what I want to do is search the column "Type" for everything in range1
Then, if say, I find "Orange" in Cell A4, I want to replace whatever is in Cell B4 with the first item in range2, which in this instance is "Spain"
And then loop over until everything has been replaced.

I hope I've made myself clear enough. If not, please let me know.
I could add some code, but what I have at the moment are parts of a whole. And I'm here because I can't figure out how to put them together. So any help would be appreciated.

EDIT
I need to do all of this in VBA.
 
Last edited by a moderator:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Assuming Range1 occupies only one column and Range2 is in the next column :
Code:
Dim rng As Range, cel As Range, f As Range
Set rng = Range([A2], Cells(Rows.Count, "A").End(xlUp))
For Each cel In rng
    Set f = Range("Range1").Find(cel)
    If Not f Is Nothing Then cel(1, 2) = f(1, 2).Value
Next
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top