Excel VBA - Find file with Dir based on keyword or ask user to path it -


i'm new vba, i'm pulling few macros together. need find file based on given path (stored in c2) , keyword (stored in d2) , print complete path e2 , if file not found, prompt dialog user can find (in case spell mistake made example).

what have far:

 public sub pather()      'find path file1 based on keyword1     dim file1 variant, keyword1 string, path1 string     keyword1 = sheet5.range("d2").text     path1 = sheet5.range("c2").text    file1 = dir(mainpath & path1)    while (file1 <> "")       'insert keyword below        if sheet5.range("e2") = ""       'print file1 path e2         sheet5.range("e2") = path1 & file1       ' display error message test reason       ' (change dialog script user can find file1 )       else:          msgbox "file not found."          '*** add filedialog here ***          exit sub       end if      file1 = dir   wend      end sub 

works if leave else out... when include else, if file exists, return both file path e2 , display alert. doing wrong?

also, want display alert , run following script:

dim fd filedialog dim filename string set fd = application.filedialog(msofiledialogopen) 'the number of button chosen dim filechosen integer filechosen = fd.show fd.title = "choose workbook" fd.initialfilename = "c:\test" fd.initialview = msofiledialogviewlist 'show excel workbooks , macro workbooks fd.filters.clear fd.filters.add "excel workbooks", "*.xlsx" fd.filters.add "excel macros", "*.xlsm" fd.filterindex = 1 fd.buttonname = "choose file" if filechosen <> -1 'didn't choose (clicked on cancel) msgbox "no file choosen. file wont saved .pdf" else 'get file, , open (name property 'includes path, need) filename = fd.selecteditems(1) workbooks.open (filename) end if 

is possible embed 2nd script 1st?

thanks soooo much

what input path1 in cell c2? if it's folder while loop goes on while there files go through. that's why multiple outputs (i.e. return of both possible outcomes).

also purpose of keyword1? supposed wildcard, extension, or file name?

edit:

for listing files match wildcard use:

   public sub pather()      'find path file1 based on keyword1     dim file1 variant, keyword1 string, path1 string     keyword1 = sheet5.range("d2").text     path1 = sheet5.range("c2").text    file1 = dir(mainpath & path1)    dim integer    = 1    while (file1 <> "")       'insert keyword below     debug.print file1       if cells(i, 5) = ""          if instr(file1, keyword1) > 0             'print file1 path e2               cells(i, 5) = path1 & file1             ' display error message test reason             ' (change dialog script user can find file1 )             = + 1             'exit sub         end if       else          debug.print "file not found"          msgbox "file not found."          '*** add filedialog here ***          exit sub       end if      file1 = dir     wend      end sub 

if want list first matches wildcard, remove ' symbol from: 'exit sub (so exits after first match found).


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -