Find and Replace Entire Array on one cycle

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
Right now I am cycling through each element in 2 arrays that are about 200 elements each. Is there a way where I can do this all in one action rather than the way I am currently doing it?

Code:
[COLOR=#0000ff]Sub[/COLOR] FindandReplace()
   
   [COLOR=#0000ff] Dim[/COLOR] vFindText    [COLOR=#0000ff] As Variant[/COLOR]
   [COLOR=#0000ff] Dim[/COLOR] vReplaceText  [COLOR=#0000ff]As Variant[/COLOR]
[COLOR=#0000ff]    Dim[/COLOR] rowStart     [COLOR=#0000ff] As Integer[/COLOR]
    [COLOR=#0000ff]Dim[/COLOR] rowEnd      [COLOR=#0000ff]  As Long[/COLOR]
  [COLOR=#0000ff]  Dim[/COLOR] ArrEle       [COLOR=#0000ff] As Long[/COLOR]
   
[COLOR=#000000]    rowStart = 2[/COLOR]
    rowEnd = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
   
    vFindText = Range(Cells(rowStart, 1), Cells(rowEnd, 1))
    vReplaceText = Range(Cells(rowStart, 2), Cells(rowEnd, 2))
   
    [COLOR=#0000ff]For[/COLOR] ArrEle = 1 [COLOR=#0000ff]To[/COLOR] rowEnd - 2
        Columns("H:H").Replace What:=vFindText(ArrEle, 1), Replacement:=vReplaceText(ArrEle, 1)
    [COLOR=#0000ff]Next[/COLOR] ArrEle
 
[COLOR=#0000ff]End Sub[/COLOR]

Any help would be much appreciated.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
From what I see, you are only making one pass down column A against column H. If any of the items in column A hit in column H, then it will replace that item with the corresponding item in column B. Pretty straight forward.
 
Upvote 0
JLGWhiz,

So your saying this is as optimized/efficient as its going to get?

Is there a possibility that a "modified" Vlookup would be quicker? Or is this the way to go?

The find and replace really only applies to between 2 to 20 values in column H
 
Last edited:
Upvote 0
JLGWhiz,

So your saying this is as optimized/efficient as its going to get?

Is there a possibility that a "modified" Vlookup would be quicker? Or is this the way to go?

The find and replace really only applies to between 2 to 20 values in column H
I am not a professional programmer, and still have much to learn about VBA, but at the moment I cannot think of a quicker method of replacing the values than the one you are using. You might get a little more speed if you put the search items and replacement items into an array and loop through the array rather than a range. But I doubt that the time difference would be significant unless you have a couple hundred thousand items to check. Everything is relative.
 
Upvote 0

Forum statistics

Threads
1,207,401
Messages
6,078,262
Members
446,324
Latest member
JKamlet

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