Excel VBA: How do I create a search engine using VBA that takes a cell's row and copies it onto a new tab within the same worksheet? -
i'm trying make excel focus on cell contains i've searched. if cell out of view in excel spreadsheet after search screen auto adjusts specific cell. then, need take in cell's row , have automatically copy new tab within same excel spreadsheet. rows copied in second tab need start column in row #5 , continue on. below code have far, i'm not familiar vba i've been working @ it. or insight appreciated.
`option explicit sub findwhat() dim sfindwhat string dim sh1 worksheet dim sh2 worksheet dim sh3 worksheet dim search range dim addr string dim nextrow long dim cl range set sh1 = thisworkbook.sheets("sheet1") set sh2 = thisworkbook.sheets("sheet2") set sh3 = thisworkbook.sheets("sheet3") '// row start pasting data on sheet3 nextrow = 5 each cl in intersect(sh1.usedrange, sh1.columns("a")).cells '// value we're looking sfindwhat = cl.value '// find value in sheet2: sh2.usedrange set search = .find(sfindwhat, lookin:=xlvalues, searchorder:=xlbyrows, searchdirection:=xlnext) if search nothing '// out of here if value not found '// not exit sub, we'll proceed next cell in column 'exit sub else '// make sure next row in sh3.column("k") empty while sh3.range("k" & nextrow).value <> "" nextrow = nextrow + 1 wend '// paste row in column k of sheet 3: search.resize(1, 12).copy destination:=sh3.range("k" & nextrow) end if end next end sub
try that:
dim sh1 worksheet dim sh2 worksheet dim sh3 worksheet dim rng range dim idrng range dim srcrng range dim search range dim lrow1 long dim lrow2 long dim lrow3 long set sh1 = thisworkbook.sheets("plan1") set sh2 = thisworkbook.sheets("plan2") set sh3 = thisworkbook.sheets("plan3") lrow1 = sh1.range("a" & rows.count).end(xlup).row if lrow1 < 4 lrow1 = 4 set idrng = sh1.range("a4:a" & lrow1) 'dynamic id's range lrow2 = sh2.range("l" & rows.count).end(xlup).row if lrow2 < 4 lrow2 = 4 set srcrng = sh2.range("l3:l" & lrow2) 'dynamic sheet2 search range each rng in idrng set search = srcrng.find(what:=rng, lookin:=xlvalues) if not search nothing lrow3 = sh3.range("k" & rows.count).end(xlup).row if lrow3 < 5 lrow3 = 5 sh2.range(search.address).entirerow.copy sh3.range("k" & lrow3) 'dynamic paste range else msgbox rng & " not found.", vbinformation, sh1.name end if next rng
remember change set sh1 = thisworkbook.sheets("plan1")
, set sh2 = thisworkbook.sheets("plan2")
, set sh3 = thisworkbook.sheets("plan3")
name of sheets.
this code has dynamic ranges id's column (sheet1), search's column (sheet2) , paste's column (sheet3), identify automatically in range last data is.
Comments
Post a Comment