Created
March 5, 2018 22:25
-
-
Save thomaswilburn/e9170f22296c8d34c3ba3f98b68d2e48 to your computer and use it in GitHub Desktop.
Scripting Excel to write CSV from VBA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Class module CSVWriter | |
Private Excel As Object | |
Private sheet As Object | |
Private book As Object | |
Private rowNum As Integer | |
Private Sub Class_Initialize() | |
Set Excel = CreateObject("Excel.Application") | |
Set book = Excel.Workbooks.Add | |
Set sheet = book.ActiveSheet | |
rowNum = 1 | |
End Sub | |
Public Sub WriteRow(ParamArray values() As Variant) | |
For c = 0 To UBound(values) | |
sheet.Cells(rowNum, c + 1) = values(c) | |
Next c | |
rowNum = rowNum + 1 | |
End Sub | |
Public Sub Save(path As String) | |
If Len(Dir(path)) > 0 Then | |
Kill path | |
End If | |
sheet.SaveAs FileName:=path, FileFormat:=6 | |
book.Saved = True | |
End Sub | |
Public Sub Quit() | |
book.Close | |
Excel.Quit | |
Set Excel = Nothing | |
Set sheet = Nothing | |
Set book = Nothing | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment