excel vba - vba - find matches cells and copy it - optimizing code -


i have col d(in sheet1 called students), col a (in sheet2 called students too)and col b(in sheet2 called age).in cold there lot of similar students name , sorting z.

i want loop col d once , if exists match students name in cola (sheet2), want copy in cold(sheet1) b column(sheet2)that matches cola(sheet2).

i sorted cold because don't want loop every similar students name. mean: if student name (for example: andrew)in cold matches student name ('andrew')in cola, copy colb(matches col a- example andrew has 15 years old) in cold(sheet1). , if string 'andrew' repeated in cold (i found in cells), don't loop again colb(sheet2), copy value first string.

for example:

(sheet1)

cold: students:     row1: andrew          row2: andrew          row3: andrew          row4: andrew          row5: andrew          row6: andrew          row7: ben          row8: ben          row9: edoardo          row10: helen          row11: leonardo          row12: leonardo          row13: robert          rown: ..   

(sheet2)

      students:  colb: age:        row1: michael          16          row2: timoth           20          row3: andrew           15          row4: edoardo          19          row5: ben              13          row6: robert           24          row7: helen            17     

what want:

(sheet1)

cold: students:         row1: 15          row2: 15          row3: 15          row4: 15          row5: 15         row6: 15          row7: 13          row8: 13          row9: 19          row10: 17          row11: 0          row12: 0          row13: 24          rown: ..      

i have code finds matched students name , put age in col d(sheet1):

set rngsearch = sheets("sheet1").range("d:d") each rngsearch in drng     set rngfound = arng.find(what:=rngsearch, lookin:=xlvalues, lookat:=xlwhole,   searchorder:=xlbyrows, searchdirection:=xlnext, matchcase:=false, searchformat:=false)     'if studentname found:      if not rngfound nothing          rngsearch.offset(0, 0) = rngfound.offset(0, 1)      else          rngsearch.offset(0, 0) = "-"        end if next 

can me optimize code, please?


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -