What version of excel are you using?
If you're using xl2k or higher:
Option Explicit
Sub testme2k()
Dim myKeyWords As Variant
Dim myRng As Range
Dim myCell As Range
Dim iCtr As Long
myKeyWords = Array("Q Line", "120 VAC")
With ActiveSheet
Set myRng = Selection
For Each myCell In myRng.Cells
For iCtr = LBound(myKeyWords) To UBound(myKeyWords)
myCell.Value = Replace(expression:=myCell.Value, _
Find:=UCase(myKeyWords(iCtr)), _
Replace:=" ", _
Start:=1, _
Count:=-1, _
compare:=vbTextCompare)
Next iCtr
myCell.Value = Application.Trim(myCell.Value)
Next myCell
End With
End Sub
Replace was added in xl2k. And that has an option to string comparisons and
ignore the case.
For xl97, you can use application.substitute but that is case sensitive:
"Q Line" won't match "q LiNE"
Option Explicit
Sub testme97()
Dim myKeyWords As Variant
Dim myRng As Range
Dim myCell As Range
Dim iCtr As Long
myKeyWords = Array("Q Line", "120 VAC")
With ActiveSheet
Set myRng = Selection
If myRng.Cells.Count > 1 Then
'do nothing
Else
'make it a multicell range
Set myRng = Union(myRng, _
.Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 1))
End If
For iCtr = LBound(myKeyWords) To UBound(myKeyWords)
myRng.Replace what:=myKeyWords(iCtr), _
replacement:=UCase(myKeyWords(iCtr)), lookat:=xlPart, _
MatchCase:=False
Next iCtr
For Each myCell In myRng.Cells
For iCtr = LBound(myKeyWords) To UBound(myKeyWords)
myCell.Value = Application.Substitute(myCell.Value, _
UCase(myKeyWords(iCtr)), _
" ")
Next iCtr
myCell.Value = Application.Trim(myCell.Value)
Next myCell
End With
End Sub
Just to make clear...
myrng.replace will work in both versions.
But this:
myCell.Value = Replace(expression:=myCell.Value, _
Find:=UCase(myKeyWords(iCtr)), _
Replace:=" ", _
Start:=1, _
Count:=-1, _
compare:=vbTextCompare)
needs xl2k or higher.