vba - Copy/Rename Batch of Files, based on data in a worksheet -


i have list in column hyperlinks of file-names pdf files on shared network drive gets filtered , sorted often. want run macro on selected filtered range excluding hidden rows in table. macro copy files new location, , rename them based on data in worksheet.

column contains hyperlinks file names including extension (e.g. cell "a3" contains "15-p980_vendor_15169_.pdf") column b , e contains text based on formula extract text file name. underscores separators. (column c , d hidden , not used). so, cell "b3" contains "15-p980", , cell "e3" contains "vendor" file name that's in column a.

enter image description here

trying rename file in each row what's in cell(, 5) + "_" + cell(, 2) in selected range rows.

i'm selecting range =$a$3:$e$6.

i getting object required error. i'm having trouble writing for each part. obtaining file path, sourcepath =. figure have obtain hyperlink address that's in column a, extract file path that, not sure how code that. appreciated.

sub copyfile() thisworkbook.activesheet.unprotect     on error goto errhndl     dim xtitleid string     dim sourcepath string, destpath string     dim sourcefile string, destfile string, sourceextension string     dim rng range, cell range, row range       destpath = "c:\users\\desktop\test\dst"     sourcefile = ""     destfile = ""      xtitleid = "copy/rename files"     set rng = thisworkbook.activesheet.application.selection     set rng = thisworkbook.activesheet.application.inputbox("range", xtitleid, rng.rows, type:=8)     set addr = rng.cells(, 1)      each row in rng.rows       sourcepath = addr.hyperlinks(1).address        sourceextension = split(row.cells(, 1), ".")(1)       sourcefile = sourcepath + row.cells(, 1)       destfile = destpath + row.cells(, 5) + "_" + row.cells(, 2) + "." +  sourceextension       file.copy sourcefile, destfile, false     next row       msgbox "operation successful.", vbokonly + vbinformation, "done"     exit sub  errhndl:     msgbox "error happened while working on: " + vbcrlf + _         sourcefile + vbcrlf + vbcrlf + "error " + _         str(err.number) + ": " + err.description, vbcritical + vbokonly, "error"  end sub 

i'm not 100% sure of you're trying accomplish in order extract filename , path separately, instead of going through range objects, took approach of looping through hyperlink collection...

for each linky in rng.hyperlinks      sourcepath = left(linky.address, len(linky.address) - len(linky.texttodisplay))      sourceextension = ".pdf" 'split(row.cells(, 1), ".")(1)     sourcefile = linky.address     destfile = destpath + cells(linky.parent.row, 5) + "_" + cells(linky.parent.row, 2) + sourceextension     fso.copyfile sourcefile, destfile, false next linky 

you'll have careful possible file duplication, raise error. also, noticed need add \ @ end of destpath.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -