ProphetofProfit
New Member
- Joined
- Feb 28, 2011
- Messages
- 28
I'm currently running a macro which identifies the value of a cell, then searches for this value in another long list of values, and then does some standard copying and pasting. However, it's taking ages, and I have a lot of cells to scan. I'd like to know which is the fastest way of searching for value in VBA, because what I've got now is unimpressive in it's speed. The code works fine, just a little slow. Would a Do Loop be quicker? I used the method below because I already had some code using the same method which worked fine and I screw up most of the VBA code I write so I played it safe.
Btw the range is from row 2 to row 300, and needs to be searched about 30000 times, which is why the speed is a problem.
1st part:
2nd part:
Btw the range is from row 2 to row 300, and needs to be searched about 30000 times, which is why the speed is a problem.
1st part:
Code:
'Set range
Dim ref
For ref = Sheets("parameters").Range("B13") To Sheets("parameters").Range("B14")
'Importing references
If Sheets("list").Cells(r, 3).Value = Sheets("reference").Cells(ref, 1).Value Then
Sheets("list").Cells(r, 20).Value = Sheets("reference").Cells(ref, 2).Value
End If
Next ref
Code:
'Importing numbers back to references
For ref = Sheets("parameters").Range("B13") To Sheets("parameters").Range("B14")
If Sheets("list").Cells(r, 3).Value = Sheets("references").Cells(ref, 1).Value Then
Sheets("references").Cells(ref, 7).Value = Sheets("references").Cells(ref, 7).Value + 1
Sheets("references").Cells(ref, 8).Value = Sheets("references").Cells(ref, 8).Value + Sheets("list").Cells(r, 6).Value
Sheets("references").Cells(ref, 9).Value = Sheets("references").Cells(ref, 9).Value + Sheets("list").Cells(r, 7).Value
Sheets("references").Cells(ref, 10).Value = Sheets("references").Cells(ref, 10).Value + Sheets("list").Cells(r, 31).Value
Sheets("references").Cells(ref, 11).Value = Sheets("references").Cells(ref, 11).Value + Sheets("list").Cells(r, 32).Value
End If
Next ref