Excel VBA: Error in file format when saved using VBA -


i trying format contents of excel file, , automatically save in specified location specified name via dialog box. have codes below, experiencing issues file format after save file. issue excel prompts me:

error received

this code allows me format excel file format require, , automatically shows location , file name want save in. codes allows me save me excel file successfully. however, when try open it, tells me file corrupted, or extension wrong.

does know why experiencing error? thanks!

code:

option explicit  sub externalratingchangefile()  'declare data type of variables dim wks worksheet dim lastcol integer dim lastrow long dim icol integer dim irow long dim sfilename string dim fdlg filedialog dim xlsxfileformat xlfileformat  'set wks current active worksheet set wks = activeworkbook.activesheet set fdlg = application.filedialog(msofiledialogfilepicker)  'set location save file variable sfilename = "h:\testing file\rating change - " + format(date, "yyyymmdd") 'xlsxfileformat = xlfileformat.xlopenxmlworkbook  'within current active worksheet, identify last interested row , column of data 'any values such 'a', '1' or '%' considered values. spaces (spacebars) not considered values. wks     .cells(1, 1).currentregion         lastcol = .columns.count         lastrow = .rows.count     end      'select interested cells , insert borders around interested fields     .usedrange.borders.linestyle = xlcontinuous     .usedrange.columns.autofit end  'inserting row @ top input date range("a1").entirerow.insert  'input today's date wks.range("a1").value = "date: " + format(date, "dd mmmm yyyy")  'save .xlsx file in specific location stated earlier  set fdlg = application.filedialog(msofiledialogsaveas) fdlg     .initialfilename = sfilename     .show  'if there errors in code, set wks nothing , end process on error goto err_handler     wks.saveas (fdlg.selecteditems(1)) end  'system to/not display alerts notify users replacing existing file. application.displayalerts = true  err_handler: 'set wks default value set wks = nothing  end sub 

i assume starting csv or other non excel format (text file etc...)

if change line saves file

wks.saveas (fdlg.selecteditems(1)) , fileformat:=xlopenxmlworkbook 

that force vba save file in "correct" format. better pick format selected in dialogue dont think returned except in file extension. detect , choose matching format if saving xlsx use above method.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -