excel - VBA code to change file format of multiple files in a folder -


the following code below changes file format of multiple files saved .xml .xlsx files. opens files in specific folder , "saves as" .xlsx. don't know how make run on files in target folder. of pointing 1st file in folder.

sub m_convertformat() ' ' m_convertformat macro '  '  dim wb workbook dim sht worksheet dim mypath string dim myfile string dim myextension string dim fldrpicker filedialog  'optimize macro speed application.screenupdating = false application.enableevents = false application.calculation = xlcalculationmanual  'retrieve target folder path user set fldrpicker = application.filedialog(msofiledialogfolderpicker)  fldrpicker     .title = "select target folder"     .allowmultiselect = false     if .show <> -1 goto nextcode     mypath = .selecteditems(1) & "\" end  'in case of cancel nextcode: mypath = mypath if mypath = "" goto resetsettings  'target file extension (must include wildcard "*") myextension = "*.xls"  'target path ending extention myfile = dir(mypath & myextension)  'loop through each excel file in folder while myfile <> ""     'set variable equal opened workbook     set wb = workbooks.open(filename:=mypath & myfile)           'change format          activeworkbook.saveas filename:= _         "s:\xyz\abc.xlsx" _         , fileformat:=xlopenxmlworkbook, createbackup:=false          end      'save , close workbook     wb.close savechanges:=true      'get next file name     myfile = dir loop  'message box when tasks completed msgbox "task complete!"   end sub 

there few things in code need adjusting in order work text wrote described. see refactored code below.

'target file extension (must include wildcard "*") myextension = "*.xml" `- since want open xml files save xlsx 

then change

 'change format          activeworkbook.saveas filename:= _         "s:\xyz\abc.xlsx" _         , fileformat:=xlopenxmlworkbook, createbackup:=false 

to

   'change format     wb.saveas filename:= wb.path & "\" replace(wb.name,".xml",".xlsx"), _             fileformat:=xlopenxmlworkbook, createbackup:=false 

then delete this: end with


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -