• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to reduce time for below mentioned loop

ThrottleWorks

Excel Ninja
Hi,

I am running below mentioned loop in my code.

The loop takes around 1-2 minutes to run. Speed depends on the system speed of that particular time.

Is there better option to reduce the time required or can the below mentioned code be better.

Please help. I will try to explain what the code does below. Sorry for not uploading sample file.

The range Rng5 is in Sheet1.
From Sheet 1 I have defined non - blank cells of Column A as Rng5.

The loop runs for each non blank value from Column A.
Each value from the range is searched in Sheet 2.

Once macro finds the value it copies Offset(0, -6).value from the active cell of Sheet 2 and paste it in Sheet 1.

Prior to running the macro Sheet 2 is active where records are to be searched.

Once again sorry for not uploading file.

PS - The macro took 1 minute for searching 25 records.

Code:
For Each rn In Rng5
        Range("A1").Select
        Cells.Find(what:=rn, After:=ActiveCell, LookIn:= _
        xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Offset(0, -6).Copy Destination:=rn.Offset(0, 6)
Next rn
 
Last edited:
Hi,

I don't know it will speed up or not but i like that way.

Why u are looking in xlFormulas ?

Code:
For Each rn In Rng5
    rn.Offset(0, 6).Value = Sheet2.Cells.Find(what:=rn, LookIn:= _
        xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, -6).Value
Next rn
 
Why u are looking in xlFormulas ?
If there are no formulas, .Find will look in the values, but much more importantly, using xlFormulas will search hidden rows too, whereas xlValues will only search among non-hidden rows (eg. filtered data).

A considerable time saving will probably be achieved by searching only the areas you expect to find rn. Currently you're searching the whole sheet:
.cells.find(…
but it's obvious that when you do find it that you want to copy cells 6 columns offset to the left - so I'd guess that you would want a result to be found among those 6 columns.
I expect that you only want to search one column for rn, so you could use:
.columns("G").find(…

Better still, a single column is still an awful lot of cells to search in versions of Excel after 2007, you can narrow it down further:
Code:
Set RangeToSearch = intersect(sheet2.usedrange,sheet2.columns("G"))
RangeToSearch.find(…
 
Last edited:
If there are no formulas, .Find will look in the values, but much more importantly, using xlFormulas will search hidden rows too, whereas xlValues will only search among non-hidden rows (eg. filtered data).

I know that!!
I asked as if there not an important is to use xlFormulas then simply use filter and copy-paste instead of loop
 
Back
Top