简体   繁体   中英

VBA replace text without replacing other words that contain similar text

I'm trying to use "*" as a wildcard for data in Column F. All the data is in the same format and length as "Pie**" where each "*" represents (0-9)

Example: Pie22, Pie71, Pie15, Hot22, Hot41, Hot98

When I try the code below, I run into the issue where CoffeeXX is finding and replacing "Hot" in "hotdog" and it becomes "chickendog".

Is there a was to fix this so that the .find sees the wildcard instead of just picking anything that contains the text?

Set PieXX = Columns("F").Find(What:="Pie**", Lookin:=xlvalues)
If not PieXX is nothing then
Columns("F").Replace What:"Pie**", replacement:="Hotdog", _
SearchOrder:=xlbyrows, Matchcase:=False, SearchFormat:=False, Replaceformat:=false
End If

    Set CoffeeXX = Columns("F").Find(What:="Hot**", Lookin:=xlvalues)
If not CoffeeXX is nothing then
Columns("F").Replace What:"Hot**", replacement:="Chicken", _
SearchOrder:=xlbyrows, Matchcase:=False, SearchFormat:=False, Replaceformat:=false
End If
For each cell in Range("F1", Range("F1").End(xlDown))
    If cell.value like "pie??" then cell.Replace "pie", "hi", LookAt:=xlPart
Next

This is untested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM