Save numbered versions of excel files based on folder contents -


i need code saves incrementally numbered versions of file based on whether named files exist in specified folder.

for example,

  1. check prescence of open file, named "inv_dec_2015.xlsx" in folder named "reports".
  2. if file exists, check "inv_dec_2015_v1.xlsx" in "reports".
  3. if file exists, check "inv_dec_2015_v2.xlsx" in "reports".
  4. if file exists, check "inv_dec_2015_v3.xlsx" in "reports".
  5. if file not exist, save open file "inv_dec_2015_v3.xlsx"

and on till number of versions......

i found following 2 pieces of code on ron de bruin's website can used , modified bit purpose, don't know how use check pre-existing files.

would appreciate this.

sub rename_store_wbk()     dim spath string  ' enter path @ file stored      spath = activesheet.range("k1").value & activesheet.range("k2").value & ".xlsx"  ' check whether file exists calling fileexist function      if fileexist(spath) = false     activeworkbook.saveas filename:=spath, _          fileformat:=xlopenxmlworkbook, createbackup:=false     end if  end sub   function fileexist(filepath string) boolean  dim teststr string  'test file path (ie "c:\users\chris\reports\inv_dec_2015.xlsm")   on error resume next     teststr = dir(filepath)   on error goto 0  'determine if file exists   if teststr = ""     fileexist = false   else     fileexist = true   end if  end function 

see if loop added in here works you:

sub rename_store_wbk()      dim spath string      ' enter path @ file stored     spath = activesheet.range("k1").value & activesheet.range("k2").value & ".xlsx"      if not fileexists(spath)          = 1                      spath = left(spath, len(spath) - 5) & "_v" & & ".xlsx"             = + 1          loop until fileexists(spath)      end if      activeworkbook.saveas filename:=spath, _          fileformat:=xlopenxmlworkbook, createbackup:=false  end sub 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -