What would prevent this Find & Replace operation in VBA from working?

gravanoc

Active Member
Joined
Oct 20, 2015
Messages
346
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
On sheet 1 is a large table with various fields, including some office names. On sheet 2 is a list of office names I want to be replaced with an empty string. I've created a simple sub to carry this out, but nothing is happening. If I bring of the Find & Replace dialog after running the code, it will show the search term there, and if I try to F&R manually, it can't find anything. However, if I delete the search term and re-type it, then it will work. Any thoughts? Since I'm using wildcards, it seems like it should capture everything in the cell where the search term is present.

VBA Code:
Sub FinderAndReplacer()

    Dim sTerm As String
    Dim rngA As Range
    Dim i As Long
    
    Set rngA = Sheet1.ListObjects(1).DataBodyRange.Cells
    Set rngB = Sheet2.Range("C4:C171")
    
    For i = 0 To rngB.Cells.Count - 1
        sTerm = rngB.Cells(i + 1, 1).Value
        sTerm = "*" & Mid(sTerm, 2, Len(sTerm) - 2) & "*"
        rngA.Replace What:=sTerm, Replacement:=""
    Next i
    
End Sub

Sheet 2 Office Names
1671356930767.png


Example of Office Names on Sheet 1
1671356985898.png
 

Attachments

  • 1671356971876.png
    1671356971876.png
    8.8 KB · Views: 3

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
One thing to check for is leading or trailing spaces on the data from either sheet. Or perhaps that is what this is about?
Mid(sTerm, 2, Len(sTerm) - 2)

Another possibility is the the characters that appear as a space character are not the same on each sheet. For example, here A5 and B5 appear identical - but they are not as evidenced by the formula results in row 6.

22 12 18.xlsm
AB
5Best Choice RealtyBest Choice Realty
632160
Sheet2 (2)
Cell Formulas
RangeFormula
A6:B6A6=CODE(MID(A5,5,1))
 
Upvote 0
One thing to check for is leading or trailing spaces on the data from either sheet. Or perhaps that is what this is about?
Mid(sTerm, 2, Len(sTerm) - 2)

Another possibility is the the characters that appear as a space character are not the same on each sheet. For example, here A5 and B5 appear identical - but they are not as evidenced by the formula results in row 6.

22 12 18.xlsm
AB
5Best Choice RealtyBest Choice Realty
632160
Sheet2 (2)
Cell Formulas
RangeFormula
A6:B6A6=CODE(MID(A5,5,1))
Thanks, I will give it a try.
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,085
Members
449,064
Latest member
MattDRT

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