+ Reply to Thread
Results 1 to 8 of 8

VBA Export to CSV with Specified File Name

Hybrid View

DomSza VBA Export to CSV with... 08-05-2015, 06:38 AM
Richard Buttrey Re: VBA Export to CSV with... 08-05-2015, 06:52 AM
DomSza Re: VBA Export to CSV with... 08-05-2015, 07:03 AM
Richard Buttrey Re: VBA Export to CSV with... 08-05-2015, 07:22 AM
DomSza Re: VBA Export to CSV with... 08-05-2015, 07:47 AM
Richard Buttrey Re: VBA Export to CSV with... 08-05-2015, 09:16 AM
  1. #1
    Registered User
    Join Date
    04-09-2015
    Location
    Manchester, England
    MS-Off Ver
    2010
    Posts
    39

    VBA Export to CSV with Specified File Name

    Hi All,

    I'm looking for VBA code that will export the current worksheet to a CSV file, save it in a pre-determind file path, but allow the user to enter a custom file name.

    The code that I'm currently using (below), works completely fine for exporting and saving in the correct path, but the user is unable to specify a file name. Any ideas?

    Sub CopyToCSV()
    Dim MyPath As String
    Dim MyFileName As String
    Application.DisplayAlerts = False
    'The path and file names:
    MyPath = "MY PATH HERE"
    MyFileName = "CSV_Export_" & Format(Date, "ddmmyy")
    'Makes sure the path name ends with "\":
    If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
    'Makes sure the filename ends with ".csv"
    If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
    'Copies the sheet to a new workbook:
    Sheets("CURRENT WORKSHEET").Copy
    'The new workbook becomes Activeworkbook:
    With ActiveWorkbook
    'Saves the new workbook to given folder / filename:
        .SaveAs Filename:= _
            MyPath & MyFileName, _
            FileFormat:=xlCSV, _
            CreateBackup:=False
    'Closes the file
        .Close False

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA Export to CSV with Specified File Name

    Hi,

    One way would be to prompt the user for the name with a VBA Input box.

    e.g.

    MyFileName = InputBox("Enter file name") & " " & Format(Date, "ddmmyy")

    alternatively have the user record the file name in a cell and use that. i.e.

    MyFileName = Sheet1.Range("A1") & " " & Format(Date, "ddmmyy")
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    04-09-2015
    Location
    Manchester, England
    MS-Off Ver
    2010
    Posts
    39

    Re: VBA Export to CSV with Specified File Name

    Thanks a lot richard, that worked perfectly!

    One more thing, if you've got a spare minute. The sheet I'm exporting is a Pivot Table, so lines 1 through 8 are blank or filters. Is there a way I can export only the data from A9 to the end of my data?

    Thanks for your help,
    Dom

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA Export to CSV with Specified File Name

    Hi Dom,

    Immediately after

    Sheets("CURRENT WORKSHEET").Copy
    put
    Range("A1:A8").EntireRow.Delete

  5. #5
    Registered User
    Join Date
    04-09-2015
    Location
    Manchester, England
    MS-Off Ver
    2010
    Posts
    39

    Re: VBA Export to CSV with Specified File Name

    Hi Richard,

    Thanks for the quick response. Unfortunately, as the base spreadsheet is a Pivot Table, it's preventing me from deleting the first 8 rows. I'm receiving the below error message when running the macro.

    Is there a way to bypass this error and delete them anyway?

    Error.PNG

    Thanks,
    Dom

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA Export to CSV with Specified File Name

    In that case the only simple way I can think of is to copy the Pivot table and paste it as a range then delete the columns containing the pivot table. Something like


    Sub CopyPivotTable()
        With ActiveSheet.PivotTables("PivotTable1")
            .PivotSelect "" 
            Selection.Copy
            Range("M1").PasteSpecial (xlPasteValuesAndNumberFormats)
            Range("M1").PasteSpecial (xlPasteFormats)
           .PivotSelect ""
            Selection.ClearContents
        End With
    Columns("A:L").Delete
    'and if necessary
    Rows("1:8").Delete
    
    End Sub
    Change the pivot table name as appropriate and change "M1" to the first unused column

  7. #7
    Registered User
    Join Date
    04-09-2015
    Location
    Manchester, England
    MS-Off Ver
    2010
    Posts
    39

    Re: VBA Export to CSV with Specified File Name

    Hi Richard,

    Worked like a charm. Had to edit slightly, pasted the values in to A1 and removed the line about deleting columns A:L. See complete working code below. Thank you for all of your help.

    Sub CopyToCSV()
    Dim MyPath As String
    Dim MyFileName As String
    'The path and file names:
    MyPath = "G:\JSM & SYNERGY\JSM & SYNERGY\Dom TEST\MailMerge\CSV Files"
    MyFileName = InputBox("Enter file name") & " " & Format(Date, "ddmmyy")
    'Makes sure the path name ends with "\":
    If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
    'Makes sure the filename ends with ".csv"
    If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
    'Copies the sheet to a new workbook:
    Sheets("PIVOT").Copy
    'The new workbook becomes Activeworkbook:
    With ActiveWorkbook
    'Replaces Pivot Data with Values only
    With ActiveSheet.PivotTables("PivotTable1")
            .PivotSelect ""
            Selection.Copy
            Range("A1").PasteSpecial (xlPasteValuesAndNumberFormats)
    'Deletes rows 1 to 8
            Rows("1:8").Delete
    End With
    'Saves the new workbook to given folder / filename:
        .SaveAs Filename:= _
            MyPath & MyFileName, _
            FileFormat:=xlCSV, _
            CreateBackup:=False
    'Closes the file
        .Close False
    End With
    End Sub
    Kind regards,
    Dom

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Export Cells to Text and automatically have File Name and File Type Selected
    By bloomingcarrot in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-18-2014, 03:37 PM
  2. [SOLVED] Trying to export a report to .XML file but no data being included in the file
    By steveallany2k6 in forum Access Tables & Databases
    Replies: 2
    Last Post: 04-18-2013, 07:16 AM
  3. Export To Inf file & Zip the Files from the given file paths picking every 10 rows
    By vivekhalder in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2012, 03:59 AM
  4. Export Excel records to seperate text file & compress it to protecte ZIP file
    By firedragon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2012, 10:47 AM
  5. Export excel file to semicolon delimited text file
    By capitan in forum Excel General
    Replies: 5
    Last Post: 04-06-2005, 11:07 PM
  6. Export excel file to semicolon delimited text file
    By capitan in forum Excel General
    Replies: 5
    Last Post: 04-06-2005, 11:07 PM
  7. How do I import text file, analyze data, export results, open next file
    By Geoffro in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-06-2005, 04:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1