简体   繁体   中英

Find and Replace Text using Excel VBA

I have a file that comes in daily with order information. The vendor sends font codes that I do not want included. I am trying to remove the font codes:

It comes in like this:

PERSONALIZATION:
J#616
BTS#P47
Tim#P46
Tailored Night*#none
Macy#P46 Frank#P46

I would like the result to look like this:

PERSONALIZATION:
J
BTS
Tim
Tailored Night
Macy Frank

This is the code:

Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long

fndList = Array("#616", "#P46", "#P47", "#none")
rplcList = Array("", "", "", "")

For x = LBound(fndList) To UBound(fndList)
    For Each sht In ActiveWorkbook.Worksheets
        sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
          LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
          SearchFormat:=False, ReplaceFormat:=False
    Next sht
Next x

I get no errors but no changes are made to the text.

I would recommend NOT to use ActiveWorkbook when you don't absolutely have to.

Since you know the name of the Workbook, declare it and use it.

Dim wb as Workbook
Set wb = Workbooks("name_of_your_workbook")

And then instead of ActiveWorkbook use the variable 'wb' eg

For Each sht In wb.Worksheets

Good luck

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