SubQuoteCommaExport()' Dimension all variables.DimDestFileAsStringDimFileNumAsLongDimColumnCountAsLongDimRowCountAsLongDimDelimiterAsStringDimWrapperAsString' What delimiter to use for field separation?Delimiter=","' What character to use for wrapping each field individuallyWrapper=""""' Prompt user for destination file name.DestFile=InputBox("Enter the destination filename"_&Chr(10)&"(with complete path):","Quote-Comma Exporter","C:\")' Obtain next free file handle number.FileNum=FreeFile()' Turn error checking off.OnErrorResumeNext' Attempt to open destination file for output.OpenDestFileForOutputAs#FileNum' If an error occurs report it and end.IfErr<>0ThenMsgBox"Cannot open filename "&DestFileEndEndIf' Turn error checking on.OnErrorGoTo0' Loop for each row in selection.ForRowCount=1ToSelection.Rows.Count' Loop for each column in selection.ForColumnCount=1ToSelection.Columns.Count' Write current cell's text to file with quotation marks.Print#FileNum, Wrapper & Replace(Selection.Cells(RowCount, ColumnCount).Text, Wrapper, Wrapper & Wrapper) & Wrapper;' Check if cell is in last column.IfColumnCount=Selection.Columns.CountThen' If so, then write a blank line.Print#FileNum,Else' Otherwise, write a comma.Print#FileNum, Delimiter;EndIf' Start next iteration of ColumnCount loop.NextColumnCount' Start next iteration of RowCount loop.NextRowCount' Close destination file.Close#FileNumEndSub
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.