Find and replace using array - need to alter it

lYoung

New Member
Joined
Mar 1, 2020
Messages
35
Office Version
  1. 365
Platform
  1. Windows
'this is not my code, however I would like to use it but I am not searching multiple sheets only 1. How can I tweak the code but use it on only one sheet? I've tried many variations with no luck - help would be greatly appreciated:)

Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant


'Create variable to point to your table
Set tbl = Worksheets("Sheet1").ListObjects("Table1")


'Create an Array out of the Table's Data
Set TempArray = tbl.DataBodyRange
myArray = Application.Transpose(TempArray)

'Designate Columns for Find/Replace data
fndList = 1
rplcList = 2


'Loop through each item in Array lists
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

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


End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi lYoung,

Welcome to the MrExcel forum.

If you could tell us more about your data, such as where it is in your workbook what data do you want to compare and replace and what the criteria for replacement is. Where you want the results, is your data in range or a table (table name) and things like that, a code using an array can be easily written.

Just looking at the code you quoted tells us nothing about your needs.
 
Upvote 0
absolutely, thank you for the reply
I am trying to do a combination of a vlookup and find/replace between two sheets.
First sheet is a range, rows will vary but columns are set, and the range will always change. This sheet is contains costs for construction jobs, i.e type of costs, vendors, cost descriptions.
The second sheet has a table consisting of only two columns, this table is set, will not change. I'm searching the "Second" sheet, first column cells for the text in the "First" sheet, column "W" and replacing text in the "First" sheet, column "W", with text in 2nd column if text matches.

COLUMN WITH JOB COST DESCRIPTION: COLUMN "W"
1583078502580.png


2ND SHEET WITH TABLE WITH COST CODE DESCRIPTION AND COST CODE
1583078563794.png

Please let me know if I did not answer your question, this is my first time reaching out to a forum for assistance.

thank you
 
Upvote 0
How about
Rich (BB code):
Sub iYoung()
   Dim Ary As Variant
   Dim i As Long

   Ary = Worksheets("Sheet1").ListObjects("Table1").DataBodyRange.Value
   For i = 1 To UBound(Ary)
     Sheets("Sheet2").Range("W:W").Replace Ary(i, 1), Ary(i, 2), xlPart, , False, , False, False
   Next i
End Sub
Change sheet names to suit
 
Upvote 0
How about
Rich (BB code):
Sub iYoung()
   Dim Ary As Variant
   Dim i As Long

   Ary = Worksheets("Sheet1").ListObjects("Table1").DataBodyRange.Value
   For i = 1 To UBound(Ary)
     Sheets("Sheet2").Range("W:W").Replace Ary(i, 1), Ary(i, 2), xlPart, , False, , False, False
   Next i
End Sub
Change sheet names to suit

Rich,
I tried running the code and received this error.
1583081947056.png

your code referenced different sheets. I do not see my type-o in the syntax
1583082002673.png
 
Upvote 0
You're missing the " from the end of the sheet name.
 
Upvote 0
Do you have a sheet called Sheet4 (as seen on the tab) & a table called Table1 on that sheet?
 
Upvote 0
I was referencing the Sheet so I renamed the Sheet to the Sheet name. The code ran without errors, but replacement did not happen, i.e. APPLIANCES replaced with APP
1583085191415.png
 
Upvote 0
Can you post some sample data of both sheets using the XL2BB add-in?
 
Upvote 0

Forum statistics

Threads
1,215,504
Messages
6,125,185
Members
449,213
Latest member
Kirbito

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