vba - find columns in other sheets and copy data loop -


ive got file sheets (master, 1, 2, 3).

below can see column in master sheet - there in order id1 id2 city olt street number

i need copy master file data other sheets first of excel should find id1 column in other sheet (its mess - there not in order) , copy rows. 1. find in sheet 1 column id1 (a1) , copy data master sheet a2 2. find in sheet 1 column id2 (b1) , cop data master sheet b2 et cetera how in loop ?

sub copy_data()

   dim b range       dim lastrow integer lastrow = cells(rows.count, 3).end(xlup).row + 1  set b = sheets("1").rows(1).find("id1")  if b nothing      msgbox "id1 not found", vbinformation, "goods not found"  else      set b = range(b.offset(1), b.offset(rows.count - 1).end(xlup))     b.copy destination:=sheets("master").range("a" & lastrow)  end if  'id  dim wrb range  set wrb = sheets("1").rows(1).find("id2")  if wrb nothing      msgbox "id2 not found", vbinformation, "goods not found"  else      set wrb = range(wrb.offset(1), wrb.offset(rows.count - 1).end(xlup))     wrb.copy destination:=sheets("master").range("b" & lastrow)   end if end sub 

i need modyfy code search columns (from master file a1:q1) in other sheets , copy data master sheet. way loop it.

thanks

try below modification, let me know if expected output.

sub copy_data()      dim b range    dim lastrow integer     x = thisworkbook.sheets.count 'added    j = 1    while j <> x   lastrow = cells(rows.count, 3).end(xlup).row + 1 lastrowm = sheets("master").cells(rows.count, 2).end(xlup).row + 1 'added  set b = sheets(j).rows(1).find("id1")  if b nothing      msgbox "id1 not found", vbinformation, "goods not found"  else      set b = range(b.offset(1), b.offset(rows.count - 1).end(xlup))     b.copy destination:=sheets("master").range("a" & lastrowm)  end if  'id  dim wrb range  set wrb = sheets(j).rows(1).find("id2")  if wrb nothing      msgbox "id2 not found", vbinformation, "goods not found"  else      set wrb = range(wrb.offset(1), wrb.offset(rows.count - 1).end(xlup))     wrb.copy destination:=sheets("master").range("b" & lastrowm)   end if   j = j + 1   loop end sub 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -