Share via

Word Find and replace text

Anonymous
2024-09-20T03:22:32+00:00

Hello from Steve

Compile error:

Method or data member not found

I have highlited the error by colouring the text brown and bold ".Value"

Sub FindAndReplaceText()

Dim rng As range 

Dim searchText As String 

Dim replaceText As String 

' Set your search and replacement text 

searchText = "4 b, 5 b" 

replaceText = "4g, 5g" 

' Check if anything is selected 

If Not Selection Is Nothing Then 

    ' Loop through each cell in the selection 

    For Each rng In Selection 

        ' Replace the search text with the replacement text 

        rng.Value = Replace(rng**.Value**, searchText, replaceText) 

    Next rng 

Else 

    MsgBox "No selection found. Macro terminated." 

End If 

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2024-09-20T06:58:38+00:00

That's strange, it works fine for me. 

Please make sure you have selected the text that needs to be replaced. I modified the code based on what you shared, and in the original code, you need to select the text to replace it, so the new code retains this part of the functionality. 

Before running:

After running:

I also shared this document with you in a private message. Please check again if it runs correctly.

Thomas

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-09-20T07:28:45+00:00

    Hello Thomas

    I am very grateful, Yes I forgot to select the text.

    All the Best

    Steve

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-09-20T06:35:44+00:00

    Hello Thomas

    Firstly thank you

    Your comment also indicates that this is applicable to cells. Was this intentional? no

    I got this macro from online.

    It did not appear to me at the time it is a excel macro.

    I work in a word document.

    I have tested your macro and it did not function.

    I am hoping for that you can maybe have an alternative solution.

    I have been looking for sometime online that maybe suitable.

    All the Best

    Steve

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-09-20T05:40:53+00:00

    Hi Steve8d,

    Thanks for your post in Microsoft Community.

    Based on the code you shared, it appears that you may have incorrectly used a VBA attribute from Excel. 

    In Word VBA, the Range object does not include the .Value property; instead, you use Range.Text in Word. 

    Your comment also indicates that this is applicable to cells. Was this intentional? 

    You might want to try this modified code:

    Sub ReplaceTextInSelection()    
        Dim rng As Range
    
        Dim searchText As String 
    
        Dim replaceText As String  
    
        ' Set your search and replacement text
    
        searchText = "4 b, 5 b"
    
        replaceText = "4g, 5g"
    
        Set rng = Selection.Range    
    
        With rng.Find        
          .Text = searchText        
          .Replacement.Text = replaceText        
          .Forward = True        
          .Wrap = wdFindStop        
          .Format = False        
          .MatchCase = False        
          .MatchWholeWord = False        
          .MatchWildcards = False        
          .MatchSoundsLike = False   
         
          .Execute Replace:=wdReplaceAll    
    
        End With
    
    End Sub
    

    Looking forward to your reply!

    Best Regards,

    Thomas C - MSFT | Microsoft Community Support Specialist

    Was this answer helpful?

    0 comments No comments