Why Does Find and Replace Not work on a Table

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
516
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I would like to understand, why I cant solve this one.

I have this code
VBA Code:
Sub BMFnR()

Dim Rng1, Rng2 As Range
Dim FndList, x&
Dim lRow As Double

Sheet8.Cells(1, 1).Select
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    
'Data Range to look in
Set Rng1 = Sheet8.Range("G1:G" & lRow)

'Data Range to Replace
Set Rng2 = Sheet1.Range("D27").CurrentRegion

FndList = Rng2.Range("D27").CurrentRegion
For x = 1 To UBound(FndList)
    Rng1.Cells.Replace What:=FndList(x, 4), Replacement:=FndList(x, 5), LookAt:=xlPart
Next

Sheet1.Range("A1").Select
End Sub

which works great when doing a multi value Find and Replace on Ranges. However, I would like to change the input data to a table.

I don't understand why the same code does not work on a Table. I keep getting Type Mismatch error on this line For x = 1 To UBound(FndList)

I appreciate that this can easily be done with PQ, but I am not that familiar with PQ to do this. I would like to keep it all in VBA

Thanks for your help
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
What are you aiming for with this line:

Code:
FndList = Rng2.Range("D27").CurrentRegion
 
Upvote 0
Solution
Hi Rory, I got it to work. Thanks.

Just bythe way, what that line does is set up the range of the values to find and the values to replace.
 
Upvote 0
I know what you were trying to do, it was just odd syntax. Why do you need to offset 26 rows and 3 columns from the top of rng2 and then get the currentregion?
 
Upvote 0
Because buddy, I am a novice programmer and don't posess the skills of good folk like yourself. :)

Can I,

delete this line
FndList = Rng2.Range("D27").CurrentRegion

and change the loop to this ?

For x = 1 To UBound(FndList)
Rng1.Cells.Replace What:=Rng2(x, 4), Replacement:=Rng2(x, 5), LookAt:=xlPart
Next

because I have not tried that
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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