Friday, May 6, 2011

Buat CSV dari makro Excel

Buat CSV dari Makro EXCEL, dengan menggunakan delimiter semicolon (titik-koma).
Sebernarnya bisa saja pake save-as nya EXCEL, tapi harus ubah regional setting windowsnya.

Sub CreateCSV()

    Dim rCell As Range
    Dim rRow As Range
    Dim sOutput As String
    Dim sFname As String, lFnum As Long
       
    'Open a text file to write
    sFname = "C:\MyCsv.csv"
    lFnum = FreeFile
   
    Open sFname For Output As lFnum
    'Loop through the rows'
        For Each rRow In ActiveSheet.UsedRange.Rows
        'Loop through the cells in the rows'
        For Each rCell In rRow.Cells
            sOutput = sOutput & rCell.Value & ";"
        Next rCell
         'remove the last comma'
        sOutput = Left(sOutput, Len(sOutput) - 1)
       
        'write to the file and reinitialize the variables'
        Print #lFnum, sOutput
        sOutput = ""
     Next rRow
   
    'Close the file'
    Close lFnum
   
End Sub

Referensi : http://www.mrexcel.com/forum/showthread.php?t=134185

No comments:

Post a Comment