Search and replace values and comments on a worksheet

sofas

Active Member
Joined
Sep 11, 2022
Messages
469
Office Version
  1. 2019
Platform
  1. Windows
Welcome. I am looking for a code that enables me to search and replace words or numbers, as well as comments. By showing the first InputBox to enter the search word and showing the second InputBox to enter the value that I want. I hope I can find someone to help me with this. Search range (A2:W200)
 

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
In a copy of your sheet, try this:

VBA Code:
Sub search_and_replace()
  Dim input_s As Variant, input_r As Variant
  Dim r As Range, cell As Range
  
  input_s = InputBox("search word")
  If input_s = "" Then Exit Sub
  input_r = InputBox("Replace word")
  If input_r = "" Then Exit Sub
  
  Set r = Range("A2:W200")
  r.Replace input_s, input_r, xlPart, , False
  
  For Each cell In r.SpecialCells(xlCellTypeComments)
    cell.Comment.Text Application.Substitute(cell.Comment.Text, input_s, input_r)
  Next
End Sub

Note: If you have formats in the comments, then adjustments would have to be made in the macro, but you must show in images how you have those formats or describe with examples, if this is not the case, then you will not have problems with the macro.


:cool:
 
Upvote 0
Solution
In a copy of your sheet, try this:

VBA Code:
Sub search_and_replace()
  Dim input_s As Variant, input_r As Variant
  Dim r As Range, cell As Range
  
  input_s = InputBox("search word")
  If input_s = "" Then Exit Sub
  input_r = InputBox("Replace word")
  If input_r = "" Then Exit Sub
  
  Set r = Range("A2:W200")
  r.Replace input_s, input_r, xlPart, , False
  
  For Each cell In r.SpecialCells(xlCellTypeComments)
    cell.Comment.Text Application.Substitute(cell.Comment.Text, input_s, input_r)
  Next
End Sub

Note: If you have formats in the comments, then adjustments would have to be made in the macro, but you must show in images how you have those formats or describe with examples, if this is not the case, then you will not have problems with the macro.


:cool:
Great. Thank you. Can I add a notification msgbox when the value does not exist, in other words, search and replace?
I tried this but it didn't work when the value was found within the comment and not in the cells
Wherever the search word is found it should be replaced
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,126
Messages
6,123,198
Members
449,090
Latest member
bes000

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