Excel VBA: Loop through two columns in sheet1, look for specific names, paste rows with matching value to sheet2 -


context: new vba

task: have contact list in worksheet1 contains columns: lastname, firstname, email, phone #, , several more. have second contact list in worksheet2 (formatted same) contains approximately 500 of 1,000 names found in worksheet1 contact list updated contact information (email, phone #, etc.). i'm trying write code find names in both worksheets, , names, copy email, phone#, etc. worksheet2 (updated information) , paste corresponding location in worksheet2.

code: have far. not work.

 sub updatecontacts()   dim reference string  dim range range  dim contactlist worksheet  dim updatedcontacts worksheet   contactlist = activeworkbook.sheets("contact list")  updatedcontacts = activeworkbook.sheets("updated contacts")   reference = contactlist.range("b5", "c5").value   j = 5   = 5 updatedcontacts.cells(rows.count, 1).end(xlup).row        if updatedcontacts.range(cells(i, 2), cells(i, 3)).value = reference            updatedcontacts.range(cells(i, 4), cells(i, 17)).copy _            destination:=contactlist.range(cells(j, 4), cells(j, 17))            j = j + 1       end if     next end sub 

any appreciated!

thanks

here working solution minor improvements such option explicit, qualified references @ times, option compare text ignore capital letters when comparing names, trim ignore possible leading or trailing spaces, , creating outer loop comparison names on shtcontactlist:

option explicit option compare text  sub updatecontacts()  dim ws worksheet dim rngcell range dim long, j long dim strreference string dim shtcontactlist worksheet dim shtupdatedcontacts worksheet  each ws in thisworkbook.worksheets     select case ws.name         case "contact list"             set shtcontactlist = ws         case "updated contacts"             set shtupdatedcontacts = ws         case else             debug.print ws.name     end select next ws if shtcontactlist nothing or shtupdatedcontacts nothing     msgbox "one or more required sheet(s) not found." & chr(10) & "aborting..."     exit sub end if  j = 5 shtcontactlist.cells(shtcontactlist.rows.count, "a").end(xlup).row     strreference = trim(shtcontactlist.cells(j, 2).value2) & ", " & trim(shtcontactlist.cells(j, 3).value2)     = 5 shtupdatedcontacts.cells(shtupdatedcontacts.rows.count, 1).end(xlup).row         if trim(shtupdatedcontacts.cells(i, 2).value2) & ", " & trim(shtupdatedcontacts.cells(i, 3).value2) = strreference             shtupdatedcontacts.range(shtupdatedcontacts.cells(i, 4), shtupdatedcontacts.cells(i, 17)).copy _                 destination:=shtcontactlist.range(shtcontactlist.cells(j, 4), shtcontactlist.cells(j, 17))             j = j + 1         end if     next next j  end sub 

if code running slow might want consider using array: (1) put entire sheet shtupdatedcontacts array sheet shtcontactlist , (2) make search / comparison there. (3) finally, paste updates array sheet shtcontactlist.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -