Array loop keeps replacing data

lthldc

New Member
Joined
Aug 22, 2018
Messages
3
Hey people,

Could I ask some help on this please. I have put together a replace macro that reads a table to from an array to then find and replace data. Problem is, it keeps looping and replacing the data over and over.

Help would be apprecited. I'm basiclly searching a list of 1-4 digit numbers and replacing it with a barcode number. My table is kind of like "220" "R0220354878". But as the search loops and finds other numbers it keeps replacing over and over. I have 570 numbers to assign barcodes to. Any help would be appreciated as I don't want to have to manually do it with find/replace in excel due to the amount of data in the sheets.

Code:
Sub Multi_FindReplace()
Dim sht As Worksheet
Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant
Dim ReplaceCount As Long


  Set tbl = Worksheets("sh2").ListObjects("Table1")

  Set TempArray = tbl.DataBodyRange
  myArray = Application.Transpose(TempArray)
  
  fndList = 1
  rplcList = 2
  For x = LBound(myArray, 1) To UBound(myArray, 2)
    'Loop through each worksheet in ActiveWorkbook (skip sheet with table in it)
      For Each sht In ActiveWorkbook.Worksheets
              
              If sht.Name <> tbl.Parent.Name Then
              
              ReplaceCount = ReplaceCount + Application.WorksheetFunction.CountIf(sht.Cells, "*" & fnd & "*")
          
       sht.Cells.Replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _
            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
        
        End If
      Next sht
    Next x
    
MsgBox "I have replaced " & ReplaceCount & " cell(s)."
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I basiclly just need it to do 1 sweep of the sheet so it doesn't start finding the numbers within the barcode numbers and keep replacing them until it runs out of combinations.
 
Upvote 0
Hi & welcome to MrExcel
Try changing this
Code:
LookAt:=xlPart
to
Code:
LookAt:=xlWhole
 
Last edited:
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,744
Messages
6,174,252
Members
452,553
Latest member
red83

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