Skip to content

Instantly share code, notes, and snippets.

@obstschale
Created September 10, 2013 09:47
Show Gist options
  • Save obstschale/6507233 to your computer and use it in GitHub Desktop.
Save obstschale/6507233 to your computer and use it in GitHub Desktop.
From http://www.ozgrid.com/forum/showthread.php?t=76720 Excle Macro to export a range of cells into .csv file. I haven't tested it but it looks good. Anyhow I don't take any responsibility for this code.
Sub CmdButton_Click()
On Error Goto CmdButton_Click_Error
'
' export Macro
Selection.Copy
'create new workbook
Workbooks.Add
' Paste into it
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
' Note I'm from the UK so my date format might be different - dd/mm/yyyy :)
Yearstr = Mid(Date, 7, 4)
Monthstr = Mid(Date, 4, 2)
Daystr = Mid(Date, 1, 2)
Datestr = Yearstr & "_" & Monthstr & "_" & Daystr
FilenameStr = "\" & Datestr & "CSVpaste.txt"
On Error Goto Cont 'Ignore error if file cannot be deleted
Kill [Path] & FilenameStr
Cont:
On Error Goto CmdButton_Click_Error
'Save workbook as csv text file
ActiveWorkbook.SaveAs Filename:= _
[Path] & FilenameStr _
, FileFormat:=xlCSV, CreateBackup:=False
'concatenate strings into Notepad string
Notepad = "notepad.exe " & [Path] & FilenameStr
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
'start notepad with csv file
Shell Notepad, vbNormalFocus
Goto endit
CmdButton_Click_Error:
MsgBox ("Error-" & Error)
endit:
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment