excel - WS.SaveAs is to slow if programm is run by Button-Click -


i'm creating .csv-files every worksheet in workbook excel macro. works fine if run macro clicking on macro in developer-tab , run macro there.

if run macro form-button inside excel-file, creates csv-file every worksheet, no content.

here vba-code:

    'this method generates csv-files every worksheet in workbook.     sub btngeneratecsv_click()      dim ws excel.worksheet     dim savetodirectory string     dim intresult integer      on error goto heaven      'disable script unnecessary things.     application.screenupdating = false     application.enableevents = false     application.displayalerts = false      'opens file dialog choosing destination folder     intresult = application.filedialog(msofiledialogfolderpicker).show      if intresult <> 0     'if selection made, selected path saved variable.     savetodirectory = application.filedialog(msofiledialogfolderpicker).selecteditems(1) & "\"      each ws in thisworkbook.worksheets         if ws.name = "wegleitung" or startswith(ws.name, "hilfstabelle")         'do nothing because these worksheets helping tables , not used data.         else             'saving worksheet csv chosen path name of worksheet.             ws.saveas savetodirectory & ws.name, xlcsv         end if      next     thisworkbook.close     end if  'enable these script unnecessary things. finally:     application.screenupdating = true     application.enableevents = true     application.displayalerts = true  'error-handling heaven:     msgbox "couldn't save sheets csv." & vbcrlf & _         "source: " & err.source & " " & vbcrlf & _         "number: " & err.number & " " & vbcrlf & _         "description: " & err.description & " " & vbcrlf end sub  'this mmethod checks if string starts specific other string. public function startswith(str string, prefix string) boolean     startswith = left(str, len(prefix)) = prefix end function 

i'm not sure real reason of problem thought have method saveas slow finish it's tasks , gets interrupted something. think because has enough time create files not fill them data.

i tried use method doevents after saveas didn't work. tried disable events on application which, thought, possible reason interrupt saveas-method.

finally read lot of questions on stackoverflow addressing task of creating csv-files out of excel-file did not find answer problem. hope not duplicate, please correct me if is.

thanks in advance!

couple of things.

  1. instead of startswith(ws.name, "hilfstabelle") can use like operator. example like "hilfstabelle*". ways not need use separate function.
  2. you need exit code before heaven:
  3. try code mentioned below. have not tested it. let me know if face problems.

code:

sub btngeneratecsv_click()     dim ws worksheet     dim savetodirectory string     dim intresult integer      on error goto heaven      application         .screenupdating = false         .displayalerts = false     end      intresult = application.filedialog(msofiledialogfolderpicker).show      if intresult <> 0         savetodirectory = application.filedialog(msofiledialogfolderpicker).selecteditems(1) & "\"          each ws in thisworkbook.worksheets             if ws.name = "wegleitung" or ws.name "hilfstabelle*"             else                 ws.copy                 activeworkbook.saveas filename:=savetodirectory & ws.name & ".csv", fileformat:=xlcsv                 activeworkbook.close savechanges:=false             end if         next     end if finally:     application         .screenupdating = true         .displayalerts = true     end      msgbox "done"     exit sub heaven:     msgbox "couldn't save sheets csv." & vbcrlf & _     "source: " & err.source & " " & vbcrlf & _     "number: " & err.number & " " & vbcrlf & _     "description: " & err.description & " " & vbcrlf      resume end sub 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -