Does anyone know the optimised VBA code for Search and Replace Next/All that is built in Excel?

excelos

Well-known Member
Joined
Sep 25, 2011
Messages
591
Office Version
  1. 365
Platform
  1. Windows
Hello!

Does anyone know the optimised VBA code for Search and Replace Next/All that is built in Excel?

I would assume it is not just a loop or something and I would like to use this as a template for any looping!

Thanks!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Record a macro using those menu selections. Then review the macro that was created.
 
Upvote 0
How about:

VBA Code:
Sub ReplaceText_NoLoop()

    Dim rng As Range
    Dim arr As Variant
    
    Set rng = Range("A2:B" & Cells(Rows.Count, "A").End(xlUp).Row)
    arr = rng.Value2
    
    ' Replace Apple with Big Apple
    arr = Application.Substitute(arr, "Apple", "Big Apple")
    
    rng.Value2 = arr

End Sub

20230310 VBA Array Replace excelos.xlsm
ABCDEFGHIJ
1ItemDescription<--- BeforeItemDescription<--- After
2AppleThe Apple is sweetBig AppleThe Big Apple is sweet
3OrangeThe Orange is sweetOrangeThe Orange is sweet
4PearThe Pear is sweetPearThe Pear is sweet
5KiwiThe Kiwi is sweetKiwiThe Kiwi is sweet
6AppleThe Apple is sweetBig AppleThe Big Apple is sweet
7OrangeThe Orange is sweetOrangeThe Orange is sweet
8PearThe Pear is sweetPearThe Pear is sweet
9KiwiThe Kiwi is sweetKiwiThe Kiwi is sweet
Data
 
Upvote 0

Forum statistics

Threads
1,215,407
Messages
6,124,723
Members
449,184
Latest member
COrmerod

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