0

I want to export to CSV and not "Save As", so create a button which will be enabled by a macro. When clicked, it should create a .csv file of the first sheet in a specified directory and also specified name. And my original worksheet should be preserved and not Saved As.

6
  • Why not do a save copy as csv? Nov 28, 2015 at 11:00
  • Saveas csv will only convert one sheet to csv
    – Davesexcel
    Nov 28, 2015 at 11:18
  • @Davesexcel OP is only talking about one sheet and hasn't really given any reason for wanting to reinvent this particular wheel. Nov 28, 2015 at 11:21
  • That's right , so there is no reason not to use saveas..........
    – Davesexcel
    Nov 28, 2015 at 11:28
  • Maybe this question will help: stackoverflow.com/q/21568837/4996248 Nov 28, 2015 at 12:58

2 Answers 2

3

If the point is keeping the original workbook untouched then why not get creative. We can copy the sheet to another workbook and the save as .csv

Option Explicit

Sub ExportOneSheet()

    Const strFILE_NAME As String = "C:\Users\Tom\Desktop\tes.csv"

    Dim shToExport As Worksheet

    ' Set the sheet to copy
    Set shToExport = ActiveWorkbook.Sheets("Sheet1")

    ' Make a copy of the sheet, when called without argument
    ' it will create a new workbook
    shToExport.Copy
    Set shToExport = ActiveWorkbook.Sheets("Sheet1")

    ' If the file exists the delete it. This will esure that
    ' there is no previous file so the replace file thing will not show
    If Not Dir$(strFILE_NAME, vbNormal) = vbNullString Then
        Kill strFILE_NAME
    End If

    ' Use Save As and your original workbook stays untouched.
    shToExport.SaveAs strFILE_NAME, XlFileFormat.xlCSV
    shToExport.Parent.Close True

End Sub

I hope this helps :)

5
  • Let me try this, will get back to you if this works. Nov 29, 2015 at 12:01
  • Well, this works perfectly bro, as intended. Thanks a lot. Would be even more glad if there was a way to skip the Yes or No buttons for Replacement of the existing file. There might be a way through, right? Nov 29, 2015 at 12:30
  • I modified the Answer to delete the file is there is one already. So now you will not be prompted to replace the file.
    – user5412293
    Nov 29, 2015 at 16:50
  • Some error in this part If Not Dir$(strFILE_NAME, vbNormal) = vbNullString Then Nov 29, 2015 at 18:16
  • note that the full path to the file is now in this constant strFILE_NAME make sure to change it to your computer file path.
    – user5412293
    Nov 29, 2015 at 18:36
2

If you do not have commas embedded in cells, this may be enough:

Sub CSV_Maker()
   Dim r As Range
   Dim sOut As String, k As Long, M As Long
   Dim N As Long, nFirstRow As Long, nLastRow As Long

   Sheets(1).Select

   ActiveSheet.UsedRange
   Set r = ActiveSheet.UsedRange
   nLastRow = r.Rows.Count + r.Row - 1
   nFirstRow = r.Row
   Dim separator As String
   separator = ","

   MyFilePath = "C:\TestFolder\"
   MyFileName = "whatever"
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set a = fs.CreateTextFile(MyFilePath & MyFileName & ".csv", True)

   For N = nFirstRow To nLastRow
       k = Application.WorksheetFunction.CountA(Cells(N, 1).EntireRow)
       sOut = ""
       If k = 0 Then
           sOut = vbCrLf
       Else
           M = Cells(N, Columns.Count).End(xlToLeft).Column
           For mm = 1 To M
               sOut = sOut & Cells(N, mm).Value & separator
           Next mm
           sOut = Left(sOut, Len(sOut) - 1)
       End If
       a.writeline (sOut)
   Next

   a.Close
End Sub
1
  • This helps mate. But the one mentioned by Fred above, is damn simpler. Nov 29, 2015 at 12:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.