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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

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