Find and Replace Series

VanillaWafer

New Member
Joined
Oct 2, 2009
Messages
2
Good Morning,

I have a question about replacing multiple values in an Excel spreadsheet.

I have a series of text strings (roughly 100,000). These strings are composed of string-bits. Unfortunately, some of these string-bits are incorrect. I have since isolated the errors in the string-bits and created two columns listing the original incorrect bit and adjacent to that, the corrected bit.

My question is if there is a macro that will go through my sheet replacing the bits in the strings, from where they exist in my spreadsheet.

To recap, I have 100,000 populated cells and roughly 5,000 finds and replaces. Is there a way to have Excel go through my list of finds and replaces and apply them automatically?

Thank you.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Interestingly, I wrote the following routine just a couple of hours ago

Code:
Public Sub RngRepWith(Rng As Range, vRep As Variant, vWith As Variant, _
                      Optional bPart As Boolean)
   If Not IsArray(vRep) Then vRep = Array(vRep)                   'Make sure entries are arrays
   If Not IsArray(vWith) Then vWith = Array(vWith)
   Dim dDict As Object, vRng As Variant
   Dim i As Long, j As Long, e As Variant
   Set dDict = CreateObject("Scripting.Dictionary")
   For i = LBound(vRep) To UBound(vRep)                           'Assign arrays to dictionary
      dDict(vRep(i)) = vWith(i)                                   'where vRep is the Key
   Next i
   vRng = Rng.Value                                               'Write the range to an array
   For i = 1 To UBound(vRng, 1)                                   'Let i be the row num
      For j = 1 To UBound(vRng, 2)                                'and j the col num
         If bPart Then                                            'If we want partial matches,
            For Each e In dDict.Keys                              'then loop thru the keys
               If InStr(1, vRng(i, j), e, 1) Then                 'and if the rng entry includes
                  vRng(i, j) = Replace$(vRng(i, j), e, dDict(e))  'the key, then replace it
               End If                                             'with the dict item
            Next e
         Else                                                     'If we don't want partials,
            If dDict.Exists(vRng(i, j)) Then                      'then as long as the vRep
               vRng(i, j) = dDict(vRng(i, j))                     'exists, we can replace it
            End If
         End If
      Next j
   Next i
   Rng.Value = vRng
   Set dDict = Nothing
End Sub

I haven't tested it as yet, but I think it should be fine. Sounds like you want to use the above something like

Code:
dim rData as range, rFindAndReplaces as range
set rData = ...
set rFindAndReplaces = ...
RngRepWith rData, rFindAndReplaces.Columns(1), _
                 rFindAndReplaces.columns(2), true

assuming I've understood your question correctly. If you don't want to scan each string--i.e. if you want to replace "Cat" with "Dog" only if "Cat" is the entire string, meaning "The cat sat on the mat" won't become "The dog sat on the mat"--then just omit the True parameter at the end of the final call.
 
Last edited:
Upvote 0
Oops, forgot I declared my parameters as variants; you'll need something more like

Code:
RngRepWith rData, rFindAndReplaces.Columns(1).value, _
                  rFindAndReplaces.columns(2).value, true
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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