excel - do while loop based on a value available in cell (find) in vba -


hi writing code in vb check particular value in sheet, if value not available should go sheet take new value find, if value found have operation on sheet have below code find value in sheet if pass same in while loop condition gives compile error

find vaue code

selection.find(what:=last_received, after:=activecell, lookin:= _     xlformulas, lookat:=xlpart, searchorder:=xlbyrows, searchdirection:= _     xlnext, matchcase:=false, searchformat:=false).activate 

could 1 please me write code of while above find in loop condition if condition gives false (i,e if value not found in sheet) should use other value find

this code going put in loop

sheets("extract -prev").select     application.cutcopymode = false      activeworkbook.worksheets("extract -prev").sort.sortfields.clear   'sorting tickets     activeworkbook.worksheets("extract -prev").sort.sortfields.add key:=range( _         "c2:c2147"), sorton:=xlsortonvalues, order:=xlascending, dataoption:= _         xlsortnormal     activeworkbook.worksheets("extract -prev").sort         .setrange range("a1:ab2147")         .header = xlyes         .matchcase = false         .orientation = xltoptobottom         .sortmethod = xlpinyin         .apply     end  application.goto reference:="r1c3"        'taking last received ticket     selection.end(xldown).select     selection.copy     sheets("calc").select     application.goto reference:="yesterday_last_received"     activesheet.paste 

this code takes last ticket based on it's availablity in next sheet "extract" have take 1 ticket previous last 1 (and on).

try below code :

sub test()      dim lastrow long     dim rng range     dim firstcell string      lastrow = sheets("sheet1").range("a" & rows.count).end(xlup).row      = 1 lastrow         set rng = sheets("sheet2").range("a:a").find(what:=cells(i, 1), lookin:=xlvalues, lookat:=xlpart, searchorder:=xlbyrows)          if not rng nothing firstcell = rng.address          while not rng nothing              rng.offset(0, 1) = "found"              set rng = sheets("sheet2").range("a:a").findnext(rng)               if not rng nothing                 if rng.address = firstcell exit             end if          loop     next  end sub 

enter image description here


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -