Solved

Export/Save worksheet as csv but keep original workbook open

Posted on 2015-01-13
2
510 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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
Perfect!
Thank you!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now