excel - Save each sheet in a workbook to separate CSV files -
how save each sheet in excel workbook separate csv
files macro?
i have excel multiple sheets , looking macro save each sheet separate csv (comma separated file)
. excel not allow save sheets different csv
files.
here 1 give visual file chooser pick folder want save files , lets choose csv delimiter (i use pipes '|' because fields contain commas , don't want deal quotes):
' ---------------------- directory choosing helper functions ----------------------- ' excel , vba not provide convenient directory chooser or file chooser ' dialogs, these functions provide reference system dll ' necessary capabilities private type browseinfo ' used function getfoldername howner long pidlroot long pszdisplayname string lpsztitle string ulflags long lpfn long lparam long iimage long end type private declare function shgetpathfromidlist lib "shell32.dll" _ alias "shgetpathfromidlista" (byval pidl long, byval pszpath string) long private declare function shbrowseforfolder lib "shell32.dll" _ alias "shbrowseforfoldera" (lpbrowseinfo browseinfo) long function getfoldername(msg string) string ' returns name of folder selected user dim binfo browseinfo, path string, r long dim x long, pos integer binfo.pidlroot = 0& ' root folder = desktop if ismissing(msg) binfo.lpsztitle = "select folder." ' dialog title else binfo.lpsztitle = msg ' dialog title end if binfo.ulflags = &h1 ' type of directory return x = shbrowseforfolder(binfo) ' display dialog ' parse result path = space$(512) r = shgetpathfromidlist(byval x, byval path) if r pos = instr(path, chr$(0)) getfoldername = left(path, pos - 1) else getfoldername = "" end if end function '---------------------- end directory chooser helper functions ---------------------- public sub dotheexport() dim fname variant dim sep string dim wssheet worksheet dim nfilenum integer dim csvpath string sep = inputbox("enter single delimiter character (e.g., comma or semi-colon)", _ "export text file") 'csvpath = inputbox("enter full path export csv files to: ") csvpath = getfoldername("choose folder export csv files to:") if csvpath = "" msgbox ("you didn't choose export directory. nothing exported.") exit sub end if each wssheet in worksheets wssheet.activate nfilenum = freefile open csvpath & "\" & _ wssheet.name & ".csv" output #nfilenum exporttotextfile cstr(nfilenum), sep, false close nfilenum next wssheet end sub public sub exporttotextfile(nfilenum integer, _ sep string, selectiononly boolean) dim wholeline string dim rowndx long dim colndx integer dim startrow long dim endrow long dim startcol integer dim endcol integer dim cellvalue string application.screenupdating = false on error goto endmacro: if selectiononly = true selection startrow = .cells(1).row startcol = .cells(1).column endrow = .cells(.cells.count).row endcol = .cells(.cells.count).column end else activesheet.usedrange startrow = .cells(1).row startcol = .cells(1).column endrow = .cells(.cells.count).row endcol = .cells(.cells.count).column end end if rowndx = startrow endrow wholeline = "" colndx = startcol endcol if cells(rowndx, colndx).value = "" cellvalue = "" else cellvalue = cells(rowndx, colndx).value end if wholeline = wholeline & cellvalue & sep next colndx wholeline = left(wholeline, len(wholeline) - len(sep)) print #nfilenum, wholeline next rowndx endmacro: on error goto 0 application.screenupdating = true end sub
Comments
Post a Comment