excel - Using vba i need to find a certain Trade ID within a folder with over 1000 workbooks and then open it -
i have started using vba code, , after several hours searching web ideas or have run wall. since site seems best reponses, wandering if me figure out how find trade id consists of 3 letters either; val, div, or lif; , series of numbers.
my idea have trade id typed cell, example c4, , click button on same sheet search entire folder trade id, since unique 1 file should open maybe two.
thanks , let me know thoughts , whether possible , how long take me write code , sort of code should use!
edit:
here code far:
private sub commandbutton1_click() dim myobj object, mysource object, file variant file = dir("x:\ops\trades\repository\") while (file <> "") if instr(file, cells(3, 4)) > 0 msgbox "found " & file exit sub end if file = dir wend end sub
edit:
some code found , edited, crashes pc when run it.
'definitions dim mypath string, filesinpath string dim myfiles() string, fnum long dim mybook workbook dim calcmode long dim sh worksheet dim erroryes boolean dim cellsearchbook worksheet dim strfile string strfile = application.getopenfilename set cellsearchbook = workbooks.open(strfile).sheets(1) cellref = inputbox("please enter horseshoe cell reference search for") mypath = "f:\ops\trades\files\" 'if no files found filesinpath = dir(mypath & "*.xls") if filesinpath = "" msgbox "no files found" exit sub end if 'array myfiles filled fnum = 0 while filesinpath <> "" fnum = fnum + 1 redim preserve myfiles(1 fnum) myfiles(fnum) = filesinpath filesinpath = dir() loop application calcmode = .calculation .calculation = xlcalculationmanual .screenupdating = false .enableevents = false end 'run through files if fnum > 0 fnum = lbound(myfiles) ubound(myfiles) set mybook = nothing on error resume next set mybook = workbooks.open(mypath & myfiles(fnum)) on error goto 0 if not mybook nothing on error resume next dim ws worksheet each ws in mybook.worksheets if .protectcontents = true ws application.screenupdating = false if instr(1, ws.range("k11").value, cellref, vbtextcompare) <> 0 ws.range("h1").copy destination:=cellsearchbook.range("a10") application.cutcopymode = false else end if else erroryes = true end if end if err.number > 0 erroryes = true err.clear 'close mybook without saving mybook.close savechanges:=false else 'save , close mybook mybook.close savechanges:=true end if on error goto 0 else 'not possible open workbook erroryes = true end if next fnum end if end sub
if helps - returns array of filenames:
private function getfilelist(filespec string) variant ' returns array of filenames match filespec ' if no matching files found, returns false dim filearray() variant dim filecount integer dim filename string on error goto nofilesfound filecount = 0 filename = dir(filespec) if filename = "" goto nofilesfound 'loop until no more matching files found while filename <> "" filecount = filecount + 1 redim preserve filearray(1 filecount) filearray(filecount) = filename filename = dir() loop getfilelist = filearray exit function ' error handler nofilesfound: getfilelist = false end function
Comments
Post a Comment