vba - Compare Values in Excel Sheet1 to Values in Sheet2 -
i have excel spreadsheet in have 2 worksheets. in first worksheet in column have store names. second worksheet subset of worksheet 1 , has store names, however, in column b of worksheet 2. need compare every store name (column a) in worksheet 1 every store name (column b) in worksheet 2 , extract 2 intersect (have same store names). far, have done in vb following:
sub runme() dim lrow, x long sheets("sheet1").select lrow = range("a2").end(xldown).row each cell in range("b2:b" & lrow) x = 2 if cell.value = sheets("sheet1").cells(x, "a").value cell.entirerow.copy sheets("sheet3").range("a" & rows.count).end(xlup).offset(1, 0) end if x = x + 1 loop until isempty(sheets("sheet1").cells(x, "a")) next end sub
how can modify code above compare every store name (column a) of worksheet 1 every store name (column b) of worksheet 2?
try this
sub runme() dim cell1 range, cell2 range dim rng1 range, rng2 range set rng1 = getrange("sheet1",1) set rng2 = getrange("sheet2",2) each cell2 in rng2 each cell1 in rng1 if cell1.value = cell2.value cell2.entirerow.copy worksheets("sheet3").range("a" & rows.count).end(xlup).offset(1, 0) end if next next end sub function getrange(shtname string, colindex long) range worksheets(shtname) set getrange = .range(.cells(2, colindex), .cells(.rows.count, colindex).end(xlup)) end end function
Comments
Post a Comment