VBA: Save as CSV, no prompts, close file. Excel 2007

Posted on 2013-06-14
Last Modified: 2016-12-08
I have scoured the web and can't find definitive code for this. They all have variables I don't know how to alter.

I simply want to save the XLS file to a CSV file right in the same folder, with the same name, and then close it. I don't want to be prompted about anything. I even know how to do that part, but not how to just save it. I'm not going to paste partial code because I don't know of any that is absolutely correct.

I don't need any error handling or anything. It's just for my personal use.

Thanks so much!
Question by:Dreamboat
LVL 50

Assisted Solution

Rgonzo1971 earned 200 total points
ID: 39247936


Sub DoIt()
FilenameS = Left(ActiveWorkbook.Name, InStr(1, ActiveWorkbook.Name, ".") - 1) '& ".csv"
Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=FilenameS, FileFormat:=xlCSV, ConflictResolution:=xlLocalSessionChanges
Application.DisplayAlerts = True
ActiveWorkbook.Close False
End Sub

Open in new window

LVL 29

Accepted Solution

gowflow earned 300 total points
ID: 39248070
Well for this you have to do 2 things
1) You have to call the below sub SaveCSVnExit that you can put in a module
2) you should also add an instruction in your ThisWorkbook under Workbook Before Close event

as follows:

Copy the below code and place it in a module

Sub SaveCSVnExit()

Dim sFileName As String

sFileName = ActiveWorkbook.Path & "\" & Left(ActiveWorkbook.Name, InStr(1, ActiveWorkbook.Name, ".") - 1) & ".csv"

Application.DisplayAlerts = False
    '---> Save Current workbook just in case latest additions not saved
    '---> Now create a CSV of the active sheet
    ActiveWorkbook.SaveAs Filename:=sFileName, FileFormat:=xlCSV
Application.DisplayAlerts = True

ActiveWorkbook.Close False
End Sub

Open in new window

Then place the below instruction in ThisWorkbook

Private Sub Workbook_BeforeClose(Cancel As Boolean)
End Sub

Open in new window

Save and Exit your workbook then start it again and when you are ready just activate the macro SaveCSVnExit and check the results.

LVL 29

Expert Comment

ID: 39248187
I just clicked on your profile and noticed your just about to reach the Genius level which I congratulate you in advance and most of all appreciate you humbleness from the way you asked you question which I salute and again congratulate you for.

Please do not hesitate to ask me for any help you may need in the Excel/VBA/VB6 I will be glad to assist.

I had personally stumbled on this issue not for saving but mainly to get rid of the Excel instance that was opened after the workbook was closed and could not get it to work and it took me lot of research and trial/errors to finally got it that you need to get rid of the application just about the workbook get finally kicked out.

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 22

Author Closing Comment

ID: 39259784
I have FINALLY got to this, so sorry! Code works GREAT. Not sure about the workbook event because it's always a different workbook that comes from a customer, and we don't want to distribute macro files to customers. Anyway... love it.

As for Genius, I don't really answer questions here anymore, though I did so lots and lots of hours per day a long time ago. :) Thanks!
LVL 29

Expert Comment

ID: 39260295
good and glad we could help

Expert Comment

by:Martin Gregar
ID: 41919165
Hello, I have some problem. I want to save xlsx to csv but I want ";" not "," in csv file. And I want to save csv in another folder. Could you help me with this? I am absolutely amateur in this problem. It is possible that VBA code will be always in xlsx file?

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

840 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