Solved

Export/Save worksheet as csv but keep original workbook open

Posted on 2015-01-13
2
554 Views
Last Modified: 2015-01-13
Is there a simple way to display the save as dialogue, allow the user to select a location and then save a copy of the active sheet as a csv to the location the user supplies without closing the original workbook?

I'm using the following code, however the original workbook gets automatically closed and the newly saved csv is left open....
Public Sub subSaveAsCSV()
Dim strFolderPath As String
Dim strFilePath As String
strFolderPath = Application.ActiveWorkbook.Path & "\"
strFilePath = Application.GetSaveAsFilename(strFolderPath, "CSV (Comma delimited) (*.csv), *.csv")
If strFilePath <> "False" Then
    ' Check if the file path already exists
    If Not Dir(strFilePath) <> "" Then
        ' If it doesn't exist then save it
        GoTo EOF
    Else
        ' Trap the error and ignore it
        On Error Resume Next
        If Err.Number = 1004 Then
            On Error GoTo 0
        Else ' User presses Save
            GoTo EOF
        End If
    End If
Else
    End
End If

EOF:
    With ActiveWorkbook
        .SaveAs Filename:=strFilePath, FileFormat:=xlCSV
    End With
End Sub

Open in new window


Thanks
0
Comment
Question by:antonioking
2 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40546607
Hi,

pls try

Public Sub subSaveAsCSV()
Dim strFolderPath As String
Dim strFilePath As String
strFolderPath = Application.ActiveWorkbook.Path & "\"
strFilePath = Application.GetSaveAsFilename(strFolderPath, "CSV (Comma delimited) (*.csv), *.csv")
If strFilePath <> "False" Then
    ' Check if the file path already exists
    If Not Dir(strFilePath) <> "" Then
        ' If it doesn't exist then save it
        GoTo EOF
    Else
        ' Trap the error and ignore it
        On Error Resume Next
        If Err.Number = 1004 Then
            On Error GoTo 0
        Else ' User presses Save
            GoTo EOF
        End If
    End If
Else
    End
End If

EOF:
    Application.ScreenUpdating = False
    ActiveSheet.Copy
    With ActiveWorkbook
        .SaveAs Filename:=strFilePath, FileFormat:=xlCSV
        .Close False
    End With
    Application.ScreenUpdating = True
End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:antonioking
ID: 40546783
Perfect!
Thank you!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question