excel - vba find value then paste another into different cell in another column -


i'm running macro looks value column of 'sheet 1' in column c of sheet 2, if these match value column b of sheet 1 should copied column m of corresponding row in sheet 2.

the macro have works, because massive worksheet, loop in taking far time. because sheet 1 has around 300,000 rows , value in each instance unique. in sheet 2 there around 50,000 rows. it's been running overnight , has reached 60,000 rows in sheet 1 far

i'm no means vba expert, or intermediate i've read maybe using find faster looking match , looping?

this macro i'm using

 option explicit  sub lookupandcopy() application.screenupdating = true  dim j long, long, lastrow1 long, lastrow2 long dim sh_1, sh_3 worksheet dim myname string  set sh_1 = sheets("sheet1")  set sh_3 = sheets("sheet2")   lastrow1 = sh_1.usedrange.rows.count   j = 2 lastrow1 myname = sh_1.cells(j, 1).value    lastrow2 = sh_3.usedrange.rows.count  = 2 lastrow2     if sh_3.cells(i, 3).value = myname         sh_3.cells(i, 13).value = sh_1.cells(j, 2).value      end if      next    next j  application.screenupdating = true end sub 

if i've missed off or other detail that's needed please let me know!

you seem using columns , b in sheet1 dictionary (and accessing values linear search). why not load values dictionary objects has o(1) search? make sure project includes reference microsoft scripting runtime (tools > references in vbe if haven't done such things) try:

sub lookupandcopy()     application.screenupdating = false      dim avals new dictionary     dim long, j long, lastrow1 long, lastrow2 long     dim sh_1, sh_3 worksheet     dim myname string      set sh_1 = sheets("sheet1")     set sh_3 = sheets("sheet2")      sh_1         lastrow1 = .range("a:a").rows.count 'last row in spreadsheet         lastrow1 = .cells(lastrow1, 1).end(xlup).row 'last used row in column         'load aval dict         j = 2 lastrow1             myname = .cells(j, 1).value             if len(myname) > 0 avals.add myname, .cells(j, 2).value         next j     end      sh_3         lastrow2 = .range("a:a").rows.count         lastrow2 = .cells(lastrow2, 3).end(xlup).row 'last used row in column 3         = 2 lastrow2             myname = .cells(i, 3).value             if avals.exists(myname)                 .cells(i, 13).value = avals.item(myname)             end if          next     end     application.screenupdating = true end sub 

if have repeated values in column need store values collections of row indices value occurs, effort of setting such dictionary still better using nested loops.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -