Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Export/Save worksheet as csv but keep original workbook open

Posted on 2015-01-13
2
Medium Priority
?
823 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 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

610 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