Advertisement
CherryDT

CSV Export (Excel)

Dec 21st, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub CSVExport()
  2.     Dim SrcRg As Range
  3.     Dim CurrRow As Range
  4.     Dim CurrCell As Range
  5.     Dim CurrTextStr As String
  6.     Dim ListSep As String
  7.     Dim FName As Variant
  8.     FName = Application.GetSaveAsFilename("", "CSV File (*.csv),*.csv,TXT File (*.txt),*.txt")
  9.  
  10.     ListSep = ","
  11.     If Selection.Cells.Count > 1 Then
  12.         Set SrcRg = Selection
  13.     Else
  14.         Set SrcRg = ActiveSheet.UsedRange
  15.     End If
  16.     Open FName For Output As #1
  17.     For Each CurrRow In SrcRg.Rows
  18.         CurrTextStr = ""
  19.         For Each CurrCell In CurrRow.Cells
  20.             CurrTextStr = CurrTextStr & """" & CurrCell.Value & """" & ListSep
  21.         Next
  22.         While Right(CurrTextStr, 1) = ListSep
  23.             CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
  24.         Wend
  25.         Print #1, CurrTextStr
  26.     Next
  27.     Close #1
  28. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement