XLS to CSV conversion

B

border21

Hi evreone, I have a Excel document and I wish to do the following :

I want to make a macro that automatically saves a copy of the XL
document in CSV format and updates the CSV document whenever the XL
one is saved.

The only problem is that I am a total n00b at Excel VBA programming...


Can anyone help me out and point me toward the right direction ??

THX
 
D

Dave Peterson

You could try this:

This part goes behind the ThisWorkbook module:

Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.OnTime Now + TimeSerial(0, 0, 1), "savecsvnow"
End Sub

This part goes in a general module:

Option Explicit
Sub SaveCSVNow()

Dim wks As Worksheet
Dim newWks As Worksheet

Set wks = ThisWorkbook.Worksheets("sheet1")

wks.Copy 'to a new workbook
Set newWks = ActiveSheet
With newWks
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
.Parent.SaveAs Filename:=ThisWorkbook.Path & "\mycsvfilenamehere.csv", _
FileFormat:=xlCSV
.Parent.Close savechanges:=False
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
End With
End Sub

Adjust the worksheet name accordingly and the .csv file, too.


If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top