find and replaced macro

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,553
I'm going to convert a lot of words in a cell into numbers, maybe about 300. So I need to build a macro that can find and replace a lot of words. I only want it to apply to one column.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi Kylefoley

This May Help you.

Code:
Public Sub FindReplace()
    
    Dim rngData     As Range
    '*************************************
    'Replace the Value of the Column and sheet as per your requirment
    Const lngCol = 1
    Const strSheetName = "Sheet1"
    Const strSrch = "Find text"                      'Here Type the text to Search
    Const strReplace = "Replace Text"           'Here type any text that you want to replace after Search
    '*************************************
    With ThisWorkbook.Worksheets(strSheetName)
        Set rngData = .UsedRange
    End With
    rngData.Columns(lngCol).Replace strSrch , strReplace , xlWhole
    
End Sub

Regards
Prince
 
Upvote 0
Hi Kylefoley

This May Help you.

Code:
Public Sub FindReplace()
    
    Dim rngData     As Range
    
    Const lngCol = 1
    Const strSheetName = "Sheet2"
    Const strSrch = "exist"                      'Here Type the text to Search
    Const strReplace = "5"           'Here type any text that you want to replace after Search
    '*************************************
    With ThisWorkbook.Worksheets(strSheetName)
        Set rngData = .UsedRange
    End With
    rngData.Columns(lngCol).Replace strSrch, strReplace, xlWhole
    
    
    '*************************************
    'Replace the Value of the Column and sheet as per your requirment
    


    Const strSrch = "space"                      'Here Type the text to Search
    Const strReplace = "4"           'Here type any text that you want to replace after Search
    '*************************************
    With ThisWorkbook.Worksheets(strSheetName)
        Set rngData = .UsedRange
    End With
    rngData.Columns(lngCol).Replace strSrch, strReplace, xlWhole
    
End Sub
I tried to adjust it so that I could replace multiple words at one time but I failed.
 
Upvote 0
In above code have you tried it by Changing the value of the column ie: lngCol= ?

by default it is searching your text in Column 1 you can change it as per requirement.
 
Upvote 0
Before, it should look like this:

Screenshot2013-10-03at125253AM.png


After it should look like this:

Screenshot2013-10-03at125313AM.png
 
Upvote 0
Hope this time it make some sens for us

Code:
Public Sub FindReplace(strSheetName As String, strSrch As String, strReplace As String, lngCol)


    With ThisWorkbook.Worksheets(strSheetName)
        .UsedRange.Columns(lngCol).Replace strSrch, strReplace, xlWhole
    End With
    
End Sub


'In this sub just pass the number of the search and replace  text
Sub callFindReplace()


    FindReplace "sheet2", "Exist", "2", 1
    FindReplace "sheet2", "Space", "3", 1
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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