My macro to find and replace (in comments) is working nicely with input boxes, EXCEPT when the user enters the same value into both input boxes (one for the find text, one for the replace with text).
then it just hangs, and excel crashes. I expect its do with using application.substitute or something.
or do i need an if Findwhat = withwhat then ignore
then it just hangs, and excel crashes. I expect its do with using application.substitute or something.
or do i need an if Findwhat = withwhat then ignore
Sub comment_Header_replace()
'replaces the header of a comment, and makes it bold, and changes font size to 10.
'asks user to save and make a backup first
'SaveSheet
FindWhat = InputBox("Enter comment header text to find")
WithWhat = InputBox("Enter comment header text to replace with")
Dim FoundCell As Range
'Dim FindWhat As String
'Dim WithWhat As String
'FindWhat = "monkey"
'WithWhat = "XXX"
Do
Set FoundCell = ActiveSheet.Cells.Find(What:=FindWhat, _
After:=ActiveCell, _
LookIn:=xlComments, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If FoundCell Is Nothing Then
Exit Do
Else
FoundCell.Comment.Text _
Application.Substitute(FoundCell.Comment.Text, _
FindWhat, WithWhat)
For Each x In ActiveSheet.Comments
With x.Shape.TextFrame
.Characters.Font.Bold = False
.Characters(1, Len(WithWhat)).Font.Bold = True
.Characters.Font.Size = 10
.AutoSize = True
End With
Next x
End If
Loop
End Sub