excel - Repeat part of my sub -
i'd run part of sub, different keyword each time it's run. i'm writing this, there shorter way doing it?
i'm making category direct debits, atm cash withdrawals, , debit card purchases in column d - searching d/d, c/l, , pos respectively in column b.
i'm changing variables, typing , running same bit of code every time. feel there should way "run bit again, searchterm , searchresult changed!"
i'm sure more knowledgable help. i'm using sub can't insert sub run within it, or function? , i'm looping i'm not sure how loop fit in?
what do? can guess i'm quite new this, little js knowledge in past.
sub organisedefaultcategories() ' ' organisedefaultcategories macro ' categorise bank statement entries default inputs. run first. ' dim foundrange range, firstaddress string, searchterm variant, searchresult variant searchterm = "d/d" searchresult = "direct debit" range("b:b") set foundrange = .find(what:=searchterm, lookin:=xlvalues, _ lookat:=xlpart, searchorder:=xlbyrows, searchdirection:=xlnext, _ matchcase:=false, searchformat:=false) firstaddress = foundrange.address foundrange.offset(0, 2).value2 = searchresult set foundrange = .findnext(foundrange) loop while not foundrange nothing , foundrange.address <> firstaddress searchterm = "c/l" searchresult = "atm cash withdrawal" set foundrange = .find(what:=searchterm, lookin:=xlvalues, _ lookat:=xlpart, searchorder:=xlbyrows, searchdirection:=xlnext, _ matchcase:=false, searchformat:=false) firstaddress = foundrange.address foundrange.offset(0, 2).value2 = searchresult set foundrange = .findnext(foundrange) loop while not foundrange nothing , foundrange.address <> firstaddress searchterm = "pos" searchresult = "debit card purchase" set foundrange = .find(what:=searchterm, lookin:=xlvalues, _ lookat:=xlpart, searchorder:=xlbyrows, searchdirection:=xlnext, _ matchcase:=false, searchformat:=false) firstaddress = foundrange.address foundrange.offset(0, 2).value2 = searchresult set foundrange = .findnext(foundrange) loop while not foundrange nothing , foundrange.address <> firstaddress end end sub
give shot:
option explicit sub organisedefaultcategories() ' ' organisedefaultcategories macro ' categorise bank statement entries default inputs. run first. ' dim searchterm string dim searchresult string searchterm = "d/d" searchresult = "direct debit" finder searchterm, searchresult searchterm = "c/l" searchresult = "atm cash withdrawal" finder searchterm, searchresult searchterm = "pos" searchresult = "debit card purchase" finder searchterm, searchresult end sub sub finder(byval searchterm string, byval searchresult string) dim foundrange range dim firstaddress string range("b:b") set foundrange = .find(what:=searchterm, lookin:=xlvalues, _ lookat:=xlpart, searchorder:=xlbyrows, searchdirection:=xlnext, _ matchcase:=false, searchformat:=false) if not foundrange nothing firstaddress = foundrange.address foundrange.offset(0, 2).value2 = searchresult set foundrange = .findnext(foundrange) loop while not foundrange nothing , foundrange.address <> firstaddress end if end end sub
i created small procedure finder
takes 2 parameters, searchterm
, searchresult
, set variables , call finder
each pair.
i declared 2 string
instead of variant
. want use variant
when absolutely necessary (some functions require variant
) because can lead hard find errors, , it's slower execute.
i modified code when extracted finder
procedure ensure check result of .find
ensure have (i.e. not ... nothing
) before attempting use in way. vba not short circuit if
statements, if didn't find .find
, have still received error way had written.
congrats on using .find
instead of looping through rows making comparison! .find
faster.
Comments
Post a Comment