Skip to content

Instantly share code, notes, and snippets.

@DeflateAwning
Created November 19, 2020 21:25
Show Gist options
  • Save DeflateAwning/6c6b2a2f8a1ad59ef8f2407285f2a068 to your computer and use it in GitHub Desktop.
Save DeflateAwning/6c6b2a2f8a1ad59ef8f2407285f2a068 to your computer and use it in GitHub Desktop.
Add this VBA Macro to your PERSONAL.XLSB workbook. Run it while selecting a cell within a table to save that table to a CSV file.
Option Explicit
Sub ButterflyButton()
Dim tbl As ListObject
Dim csvFilePath As String
Dim fNum As Integer
Dim i As Long
Dim tblArr
Dim rowArr
Dim csvVal
'Set tbl = ActiveSheet.ListObjects(ActiveCell.ListObject.Name)
Set tbl = ActiveCell.ListObject
csvFilePath = Application.GetSaveAsFilename(FileFilter:="CSV UTF-8 (Comma delimited) (*.csv), *.csv")
tblArr = tbl.Range.Value
' TODO change this section to move table into new Excel workbook and save that workbox
' as a CSV for ideal encoding (UTF-8) and quoting (cells containing commas)
fNum = FreeFile()
Open csvFilePath For Output As #fNum
For i = 1 To UBound(tblArr)
rowArr = Application.Index(tblArr, i, 0)
csvVal = VBA.Join(rowArr, ",")
Print #1, csvVal
Next
Close #fNum
Set tblArr = Nothing
Set rowArr = Nothing
Set csvVal = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment