Search and replace

aerosmith

New Member
Joined
May 10, 2011
Messages
8
HI there, would there be an easy way to search a worksheet for 47 specific words within the entire sheet and replace it with null or blank,
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Give this macro a try where I have assumed the words you want to replace fill up the cells they are in (that is, no other text is in the cell with them)...

Code:
Sub DeleteWords()
  Dim Word As Variant
  Const WordList = "One,Two,Three,Four,Five,etc."
  For Each Word In Split(WordList, ",")
    Cells.Replace Trim(Word), "", xlWhole
  Next
End Sub
Simply replace my sample list of words in the WordList constant (the Const statement) with your own words.
 
Upvote 0
so THAT'S how you use Split? that's awesome. Def be using that in the future.
The more common usage of Split is to create an array and assign it to a dynamic array variable that can be used later on in your program. For example...

Code:
Dim X As Long, MyArray() As String
....
....
MyArray = Split("One,Two,Three,Four", ",")
....
....
' Let's see what's in MyArray
For X = 0 To UBound(MyArray)
  MsgBox MyArray(X)
Next
Note that Split always returns a zero-based array no matter what Option Base setting you might be using. I just mentioned that in case you were wondering why I used 0 instead of LBound(MyArray) in the For statement.
 
Upvote 0
Give this macro a try where I have assumed the words you want to replace fill up the cells they are in (that is, no other text is in the cell with them)...

Code:
Sub DeleteWords()
  Dim Word As Variant
  Const WordList = "One,Two,Three,Four,Five,etc."
  For Each Word In Split(WordList, ",")
    Cells.Replace Trim(Word), "", xlWhole
  Next
End Sub
Simply replace my sample list of words in the WordList constant (the Const statement) with your own words.



Sorry for the delay,

This is amazing, Works Exactly the way i need it, I undertand why people asked why i didint use the normal replace method, i have approx 47 items to run the script against. on a daily basis, and this works excellent.
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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