Replace a specific string in excel

Gabriell

New Member
Joined
Jan 9, 2017
Messages
23
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
Dear Members,

I need a solution to replace a specific string in an Excel file, but only when it meets certain criteria.

For example, in the database, I have different strings, in different columns, like "BRAK" and "BRAKE." The goal is to replace "BRAK" with "-", while leaving "BRAKE" unchanged.

What would be the best way to achieve this?

Thank you in advance!
Gabor
 
Dear Members,

I need a solution to replace a specific string in an Excel file, but only when it meets certain criteria.

For example, in the database, I have different strings, in different columns, like "BRAK" and "BRAKE." The goal is to replace "BRAK" with "-", while leaving "BRAKE" unchanged.

What would be the best way to achieve this?

Thank you in advance!
Gabor
Taken from this post: VBA Bulk Find and Replace or Similar

Substitute the values in the arrays FindThese and ReplaceWith as appropriate for your needs.

Test on a copy of your data as Ctrl-Z will not work.

Only Whole cell values are replaced.

VBA Code:
' Check for all cells in a workbook.
Public Sub ReplacementsE1()
Dim X As Long, FindThese As Variant, ReplaceWith As Variant
  
  FindThese = Array("Royal Mail", "CRL24", "TPN24")
  
  ReplaceWith = Array("TPN24", "Royal Mail", "TRACKED")
  
  For X = LBound(FindThese) To UBound(FindThese)
    Worksheets("Sheet2").Cells.Replace FindThese(X), ReplaceWith(X), xlWhole, , True, , False, False
  Next

End Sub

'  Check for cells in specified columns only.
Public Sub ReplacementsE2()
Dim X As Long, FindThese As Variant, ReplaceWith As Variant
  
  FindThese = Array("Royal Mail", "CRL24", "TPN24")
  
  ReplaceWith = Array("TPN24", "Royal Mail", "TRACKED")
  
  For X = LBound(FindThese) To UBound(FindThese)
    Worksheets("Sheet2").Columns("B:C").Replace FindThese(X), ReplaceWith(X), xlWhole, , True, , False, False
  Next

End Sub
 
Upvote 0
1. Replace BRAKE with something else, like §§§§§.
2. Now replace BRAK with -.
3. Then change §§§§§ back to BRAKE.
 
Upvote 0
Taken from this post: VBA Bulk Find and Replace or Similar

Substitute the values in the arrays FindThese and ReplaceWith as appropriate for your needs.

Test on a copy of your data as Ctrl-Z will not work.

Only Whole cell values are replaced.

VBA Code:
' Check for all cells in a workbook.
Public Sub ReplacementsE1()
Dim X As Long, FindThese As Variant, ReplaceWith As Variant
 
  FindThese = Array("Royal Mail", "CRL24", "TPN24")
 
  ReplaceWith = Array("TPN24", "Royal Mail", "TRACKED")
 
  For X = LBound(FindThese) To UBound(FindThese)
    Worksheets("Sheet2").Cells.Replace FindThese(X), ReplaceWith(X), xlWhole, , True, , False, False
  Next

End Sub

'  Check for cells in specified columns only.
Public Sub ReplacementsE2()
Dim X As Long, FindThese As Variant, ReplaceWith As Variant
 
  FindThese = Array("Royal Mail", "CRL24", "TPN24")
 
  ReplaceWith = Array("TPN24", "Royal Mail", "TRACKED")
 
  For X = LBound(FindThese) To UBound(FindThese)
    Worksheets("Sheet2").Columns("B:C").Replace FindThese(X), ReplaceWith(X), xlWhole, , True, , False, False
  Next

End Sub
I couldn't configure it in the right way. Thanks anyway!
 
Upvote 0

Forum statistics

Threads
1,226,795
Messages
6,193,045
Members
453,772
Latest member
aastupin

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