Solved

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

Posted on 2013-06-14
5
9,462 Views
Last Modified: 2013-06-19
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!
0
Comment
Question by:Dreamboat
  • 3
5 Comments
 
LVL 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 200 total points
Comment Utility
Hi,

Try

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

0
 
LVL 29

Accepted Solution

by:
gowflow earned 300 total points
Comment Utility
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
    ActiveWorkbook.Save
    '---> Now create a CSV of the active sheet
    ActiveWorkbook.SaveAs Filename:=sFileName, FileFormat:=xlCSV
Application.DisplayAlerts = True

ActiveWorkbook.Close False
Application.Quit
End Sub

Open in new window


Then place the below instruction in ThisWorkbook

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.Quit
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.

gowflow
0
 
LVL 29

Expert Comment

by:gowflow
Comment Utility
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.

Rgds/gowflow
0
 
LVL 22

Author Closing Comment

by:Dreamboat
Comment Utility
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!
0
 
LVL 29

Expert Comment

by:gowflow
Comment Utility
good and glad we could help
gowflow
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

771 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

11 Experts available now in Live!

Get 1:1 Help Now