Excel compare two columns from one sheet copy entire row on match to new sheet -


i looking vba code following:

  • on sheet 1, starting a2, scroll down , compare each cell, 1 @ time, each cell in second column, starting b2.
  • if there match, copy entire row of matching entry in second column sheet 2
  • if, after scrolling through column b, there no matches, insert blank row in sheet 2

here pseudocode may clarify i'm looking for:

for each cell in columna
traverse each cell in columnb
if current cell value in columna matches current cell value in columnb
copy entire row @ current columnb position
if have traversed entire columnb , have not found match
insert blank row in sheet2

here best come with, not well-versed in manipulating excel sheets:

sub rowcontent()  dim ismatch boolean ismatch = false  dim newsheetpos integer newsheetpos = 1  dim numrows integer numrows = 591  dim rowpos integer rowpos = 1  = 1 numrows 'traverse columna   j = 1 numrows 'traverse columnb     'compare contents of cell in columna cell in columnb     if worksheets("sheet1").cells(i, 1) = worksheets("sheet1").cells(j, 2)         worksheets("sheet1").cells(i, 1).copy worksheets("sheet2").cells(newsheetpos, 1)         newsheetpos = newsheetpos + 1'prepare copy next row in sheet2         ismatch = true      end if      j = j + 1 'increment j continue traversing columnb  next  'if have traverse columnb without finding match  if not (ismatch)          newsheetpos = newsheetpos + 1 'skip row in sheet2 if no match found  end if  ismatch = false next end sub 

this code not work.

many kind help.

i have made som changes code. should work pseudocode-description:

sub rowcontent()     dim ws1 worksheet     dim ws2 worksheet     dim long, j long     dim ismatch boolean     dim newsheetpos integer      set ws1 = activeworkbook.sheets("sheet1")     set ws2 = activeworkbook.sheets("sheet2")      'initial position of first element in sheet2     newsheetpos = ws2.cells(ws2.rows.count, 1).end(xlup).row      = 1 ws1.cells(ws1.rows.count, 1).end(xlup).row         ismatch = false         j = 1 ws1.cells(ws1.rows.count, 2).end(xlup).row             if ws1.cells(i, 1).value = ws1.cells(j, 2).value                 ws1.cells(j, 2).entirerow.copy ws2.cells(newsheetpos, 1)                 ismatch = true                 newsheetpos = newsheetpos + 1             end if         next j         if ismatch = false newsheetpos = newsheetpos + 1     next end sub 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -