Find Replace VBA

doug firr

Board Regular
Joined
Mar 28, 2011
Messages
140
I have a column called "specialty" with about 2,000 rows. Each row can be one of about 20 specialties (the specialties are professions, such as analyst, chef etc etc).

Each of these specialties must be re-grouped further into 5 upper level headings (e.g. office, services, trade etc)

What I have to do each reporting period is do find/replace in excel for each of the 20 categories. e.g. find analyst and replace with office.

I could do a vlookup but I want to start familiarizing myself with VBA.

Is there a piece of code some generous mr excel boards user could provide to save me from doing this each time?
 
Reading Shawnyv posts I started thinking how this could be done with a vlookup, but I know you want to replace the values. So I came up with a combination (see below) of both.

FYI. In cells A1 to A3, of sheet 1, I have Analyst, Chef and Nurse
In Sheet 2, I have Analyst, Chef, and Nurse in cells A1 to A3 and Office, Kitchen, and Hospital in cells B1 to B3.

Here is the code which will do a vlookup using array in sheet 2 and perform a Find/Replace on the same column you highlighted to do the lookup.

Sub Update()

Dim rng As Range
Dim look As Range
Dim col As Integer
Dim found As Variant

Set look = Application.InputBox(Prompt:="SELECT COLUMN TO REPLACE", Type:=8)
look.Select
Set rng = Sheets("Sheet2").Columns("A:B") 'basically a lookup table
col = 2
If look Is Nothing Then Exit Sub
For Each look In Selection
If IsEmpty(look.Value) Then GoTo Line99
found = Application.WorksheetFunction.VLookup(look, rng, col, o)
Cells.Replace What:=look, Replacement:=found, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Line99:
Next look

End Sub
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
@Shawnyv,

If I understand your code (and the setup it applies to) correctly, I believe this shorter piece of code will do the same thing...
Code:
Dim C As Range
For Each C In Sheets("Sheet2").Columns("A").SpecialCells(xlCellTypeConstants)
  Sheets("Sheet1").Cells.Replace C.Value, C.Offset(0, 1).Value
Next
The important thing to take away from my code sample is that it is almost never necessary to select sheets and cells on those sheets in order to manipulate the cells themselves in some way... you normally can refer to the sheet and/or cell object properties and methods directly rather than selecting sheets and cells only to then refer to the properties and methods of the ActiveCell instead.
 
Upvote 0
Thx for that - much more elegant & useful.

See, that's why I always feel like the dumbest guy on the board. ;)
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,673
Members
452,937
Latest member
Bhg1984

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