vba: Find "xlpart"

snjpverma

Well-known Member
Joined
Oct 2, 2008
Messages
1,584
Office Version
  1. 365
Platform
  1. Windows
I have a macro that does a find xlwhole on an array of words and replaces then accordingly.
Below is the code from a website that I modified to suit my needs.

VBA Code:
Sub Multi_FindReplace()
'PURPOSE: Find & Replace a list of text/values throughout entire workbook
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long

fndList = Array("Canada", "United States", "Mexico")
rplcList = Array("CAN", "USA", "MEX")

'Loop through each item in Array lists
  For x = LBound(fndList) To UBound(fndList)
    'Loop through each worksheet in ActiveWorkbook
      For Each sht In ActiveWorkbook.Worksheets
        sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
          LookAt:=xlwhole, SearchOrder:=xlByRows, MatchCase:=False, _
          SearchFormat:=False, ReplaceFormat:=False
      Next sht
  Next x
End Sub

The macro works absolutely fine.
However, I want to add a small line which can set my "Find (LookAt)" back to Xlpart.
Because apart from this macro I never use xlwhole to find.
So every time I run the macro, I have to open the option in Find manually and mark the match entire cell contents.
please suggest.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Just run another find with a string that you know will error and put a is nothing exit sub line afterwards at the end of the sub. Something like...
VBA Code:
Dim xRng As Range
Set xRng = Columns(1).Find("nonsense string", , xlValues, xlPart, xlByRows, xlNext)
If xRng Is Nothing Then Exit Sub

Edit: see Fluff's answer as it doesn't matter if it is at the end of the sub, it's a much simpler way
 
Last edited:
Upvote 0
Just add this after the loop
VBA Code:
   Cells.Find "", , , xlPart
 
Upvote 0
Thanks to both of you for the answers.

Just add this after the loop
VBA Code:
   Cells.Find "", , , xlPart


Fluff, would you please let me know what this code actually does?

It certainly serves the purpose (xlpart option selection) for me.

However, I just tried replacing "" with other common things. I thought the cursor will go to the cell containing the data.
However, it doesn't.

for e.g.
I used
Cells.Find 45, , , xlPart

and I wrote 45 in multiple cells, but the cursor didn't go select those cells.

Please explain.
 
Upvote 0
It just resets the LookAt to xlPart & the What to nothing & that's it. :)
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
I am curious to know why the code in post 1 works fine even when no match is found. Whereas the code in post 2 needs a statement IS NOTHING to handle the error when nothing is found.
 
Upvote 0
It is just that Microsoft decided that not finding anything to replace was a legitimate answer in replace and so coded under the bonnet the replace part of Find and Replace to handle it.
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,382
Members
448,889
Latest member
TS_711

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