Solved

Export/Save worksheet as csv but keep original workbook open

Posted on 2015-01-13
2
699 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

728 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