excel - VBA Findnext Issue -
this code works until input coding between ********** i'm trying 2 different searches @ same time. can explain i'm doing wrong? thanks
public sub swap() sheet1.range("a:a") set lastcell = .cells(.cells.count) end set foundcell = sheet1.range("a:a").find(what:=cusip, after:=lastcell) if not foundcell nothing firstaddr = foundcell.address end if until foundcell nothing account = sheet1.cells(foundcell.row, 2) ''#************************************* set foundcell2 = sheet2.range("b:b").find(what:=account) if not foundcell2 nothing firstaddr2 = foundcell2.address end if ''#********************************************* set foundcell = sheet1.range("a:a").findnext(after:=foundcell) ''#break out of loop when searched through of cusips if foundcell.address = firstaddr exit end if loop end sub
you can't 2 different finds @ same time. it's limitation of excel's object model. there's 1 find "cursor", , somwhere in b:b when try findnext in a:a. have old inefficient looping way 1 of finds. here's how loop inner find
public sub swap() dim lastcell range dim foundcell range dim foundcell2 range dim firstaddr string dim foundaddr string dim account variant const cusip string = "cusip" set lastcell = sheet1.cells(sheet1.rows.count, 1) set foundcell = sheet1.range("a:a").find(what:=cusip, after:=lastcell) if not foundcell nothing firstaddr = foundcell.address account = sheet1.cells(foundcell.row, 2) foundaddr = "" each foundcell2 in intersect(sheet2.usedrange, sheet2.columns(2)).cells if foundcell2.value = account foundaddr = foundcell2.value exit end if next foundcell2 if len(foundaddr) = 0 foundaddr = "not found" end if debug.print foundcell.address, foundaddr set foundcell = sheet1.range("a:a").findnext(after:=foundcell) loop until foundcell nothing or foundcell.address = firstaddr end if end sub
Comments
Post a Comment