vba - How to find the row number of a cell value that matches any given value -
in situation, checking each cell in range of "f". , looking see if each cell matches of cells in separate range "d". if want know row number in cell exists , copy (as information in other column of same row) sheet. if can't find match print cell not find , beside "0". how can find value of row in data exists?
dim lastrow integer lastrow = range("f65336").end(xlup).row dim c integer c = 2 each cell in range("f2:f" & lastrow) if cell.value = range("d2:" & lastrow) sheet2.range("b" & c).value = sheet1.range("d" & c).value sheet1.range("d" & c).value = sheet1.range("f" & value_of_row_of_matching_cell).value sheet2.range("c" & c).value = sheet1.range("h" & value_of_row_of_matching_cell).value else sheet2.range("b" & c).value = sheet1.range("d" & c).value sheet1.range("c" & c).value = 0 end if c = c + 1 next cell
how work?
if cell.value = range("d2:" & lastrow)
it attempting compare single cell collection of cells, , never evaluate true.
since mention looking see if each cell matches of cells in separate range "d", think need each in code:
dim lastrow integer lastrow = range("f65336").end(xlup).row dim fcell, dcell each fcell in range("f2:f" & lastrow) dim frow integer frow = fcell.row 'prepopulate default, not-found values sheet2.range("b" & frow).value = sheet1.range("d" & frow).value sheet1.range("c" & frow).value = 0 each dcell in range("d2:d" & lastrow) if fcell.value = dcell.value dim drow integer drow = dcell.row sheet2.range("b" & frow).value = sheet1.range("d" & drow).value sheet1.range("d" & frow).value = sheet1.range("f" & drow).value sheet2.range("c" & frow).value = sheet1.range("h" & drow).value end if next dcell next fcell
note above assumes ranges span same set of rows. if indeed case, suggest looking using vlookups instead.
for example, use column g perform vlookup. put in g2 , fill down:
=iferror(vlookup(f2,d:d,1,false), "")
then, in sheet2, cell b2, reference g2:
=if(sheet1!g2="","",sheet1!d2)
...and on.
Comments
Post a Comment