Change
If InStr(1, cell, "total", vbTextCompare) Then
to
If lCase(cell.Value) = "total" then
Sub ReplaceOtherStrings()
Dim s As String, cell As Range, rng As Range
Dim i As Long, ans As Long, s1 As String
Dim ans1 as String, ans2 as String
Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
ans1 = InputBox("Enter Search Term")
if len(trim(ans1)) = 0 then exit sub
ans1 = lcase(ans)
if instr(1,ans1,"total",vbTextCompare) = 1 then
ans2 = Trim(Mid(ans1,7,255))
if lcase(right(ans2,1)) = "s" then
ans2 = Left(ans2,Len(ans2)-1)
end if
Else
ans2 = Ans1
End if
For Each cell In rng
If Trim(cell) = ans1 Then
i = cell.Row - 2
Do Until Len(Trim(Cells(i, 1))) = 0
i = i - 1
Loop
cell.Select
s = "Total will be replaced by " & _
ans1 & vbNewLine _
& vbNewLine _
& "Yes: Continue" & vbNewLine _
& "No: Do Not Replace" & vbNewLine _
& "Cancel: Use a word I will provide"
ans = MsgBox(s, vbYesNoCancel, "Make a Choice")
Select Case ans
Case vbYes
Range(Cells(i + 1, 1),Cell.offset(-2,0)).Value = ans1
Case vbCancel
s1 = InputBox("Provide Word to Use?", _
"Enter Replacement", ans1)
If Len(Trim(s1)) > 0 Then
Range(Cells(i + 1, 1),Cell.offset(-2,0)).Value = s1
End If
End Select
End If
Next
End Sub
Sub ReplaceSpecifiedWordWithSpecifiedWord()
Dim s As String, cell As Range, rng As Range
Dim i As Long, ans As Long, s1 As String
Dim ans1 as Long, ans2 as Long
ans1 = InputBox("Enter Search term")
ans2 = InputBox("Enter Replacement term"
Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
For Each cell In rng
If lcase(cell.value) = lcase(ans1) Then
cell.Select
s = ans1 & " will be replaced by " & ans2 & vbNewLine _
& vbNewLine _
& "Yes: Continue" & vbNewLine _
& "No: Do Not Replace" & vbNewLine _
& "Cancel: Use a word I will provide"
ans = MsgBox(s, vbYesNoCancel, "Make a Choice")
Select Case ans
Case vbYes
cell.Value = ans2
Case vbCancel
s1 = InputBox("Provide Word to Use?", _
"Enter Replacement", Cells(i + 1, 1).Value)
If Len(Trim(s1)) > 0 Then
cell.Value = s1
End If
End Select
End If
Next
End Sub
code is untested, so you may need to do a little debugging.
--
Regards,
Tom Ogilvy
Ricky Pang said:
Tom, this code is incredible. The popup message box really helps me
know what titles are about to be changed. The override option is great.
1) This is for self improvement about VB. When I ran your code, it
searches for all titles with the word "Total" within column A. In
comparison, how would the line be different if I needed to match and
replace a non-case-sensitive "Total" only so it would bypass other
titles with additional words such as Total Operating Expenses?
2) Having finished replacing all titles named "Total", how do you
replace a range between 2 empty spaces based on another Title search
such as "Total Operating Expenses"?
[space] --- this is the 1st space, range starts here.
Non-Operating Legal
Non-Operating Repairs
Non-Operating Leasing
[space] --- this is the 2nd space, range ends here.
Total Operating Expenses --- First, search for this title.
search for "Total Operating Expenses".
jump up to the 2nd empty space, mark it as Range ends.
jump up to the 1st empty space, mark it as Range starts.
then change everything within the range to become "Operating Expense".
keep maintaining the useful popup msgbox.
3) For more title replacements in the future, is there a place in code
that I could type in a search word such as "Collection Loss" and replace
with "Bad Debt" (while mainting the useful popup msgbox)?
Your help is greatly appreciated.
Thanks again,
Ricky