VBA: Find and Replace Help

NeoSez

Board Regular
Joined
Aug 14, 2020
Messages
210
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
What is wrong with this code?
I am only searching in one column.
I want a full match with "NEST" only and not partial string matches.
For example it found NEST in "Big Bird Nest" and replaced it with "Big Bird Nest" again, so the result came out "Big Big Bird Bird Nest Nest". How do I make it only replace when it finds only one NEST in the cell?
I tried to add
VBA Code:
LookAt:=xlWhole
but it didn't work.
Other than that the VBA script works great for finding & replacing.

VBA Code:
'   Replace NEST,
    myRange.Replace what:="NEST", Replacement:="Big Bird Nest", lookat:=xlPart, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False,
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
The following will do it but you'll need to edit the code to fit your "range" :

VBA Code:
Option Explicit

Sub FindnReplace()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet
    
    Dim myString As String

    ' Change worksheet designations as needed
    Set Source = ActiveWorkbook.Worksheets("Sheet1")
 
    
    myString = "NEST"
    
    For Each c In Source.Range("A2:A1000")   ' Do 1000 rows
        If c = myString Then
           c.Value = "Big Bird Nest"
        End If
    Next c
End Sub

Tested here.
 
Upvote 0
The following will do it but you'll need to edit the code to fit your "range" :

VBA Code:
Option Explicit

Sub FindnReplace()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet
  
    Dim myString As String

    ' Change worksheet designations as needed
    Set Source = ActiveWorkbook.Worksheets("Sheet1")

  
    myString = "NEST"
  
    For Each c In Source.Range("A2:A1000")   ' Do 1000 rows
        If c = myString Then
           c.Value = "Big Bird Nest"
        End If
    Next c
End Sub

Tested here.
Thanks for the code.

However, I was hoping to just modify the script I already have as it is rather lengthy with many search and replace items. Is that possible to just modify this script? I'd do it but I'm a noob and will screw it up.

VBA Code:
Sub Replace()
    Dim lastRow As Long
    Dim myRange As Range
   
'   Find lastRow in column F
    lastRow = Cells(Rows.Count, "F").End(xlUp).Row

'   Set range to look at
    Set myRange = Range("F2:F" & lastRow)

'   Replace NEST,
    myRange.Replace what:="NEST", Replacement:="Big Bird Nest", lookat:=xlPart, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False,
 
Upvote 0
I tried to add
VBA Code:
LookAt:=xlWhole
but it didn't work.
Other than that the VBA script works great for finding & replacing.

VBA Code:
'   Replace NEST,
    myRange.Replace what:="NEST", Replacement:="Big Bird Nest", lookat:=xlPart, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False,
You shouldn't add that code, it should replace the lookat:=xlpart

VBA Code:
myRange.Replace what:="NEST", Replacement:="Big Bird Nest", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
 
Upvote 0
You shouldn't add that code, it should replace the lookat:=xlpart

VBA Code:
myRange.Replace what:="NEST", Replacement:="Big Bird Nest", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Thank you Peter. I totally missed that. Works now.
 
Upvote 0
You're welcome. Glad it is sorted. :)
BTW, Logit did suggest the same thing a moment before I posted.
 
Upvote 0
You're welcome. Glad it is sorted. :)
BTW, Logit did suggest the same thing a moment before I posted.
Yes, I saw Logit's message after I posted. Thank you for your time Logit .
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,607
Members
449,090
Latest member
vivek chauhan

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