maandag 14 december 2009

Export excel file to csv (VBA Macro)

Set Separator (;)


Sub SaveToCSVFile()

Dim fs As Object, a As Object, i As Integer, j As Integer, s As String, t As String, l As String, mn As String
Set fs = CreateObject("Scripting.FileSystemObject")
Dim newFileName As String
newFileName = ThisWorkbook.FullName
newFileName = Replace(newFileName, ".xls", ".csv")
Set a = fs.CreateTextFile(newFileName, True)
Range("A1").Select
i = ActiveCell.CurrentRegion.Columns.Count
ActiveCell.CurrentRegion.EntireRow.Delete


For rowNumber = 1 To Range("A65536").End(xlUp).Row
s = ""
Col = 1
For j = 0 To i
s = s & Cells(rowNumber, Col) & ";" 'Separator
Col = Col + 1
Next j
a.writeline s 'write line
Next rowNumber

End Sub

Geen opmerkingen:

Een reactie posten