PC Review


Reply
Thread Tools Rate Thread

Code to run macro on save

 
 
santaviga
Guest
Posts: n/a
 
      23rd Jan 2008
I have the following code in my workbook, it allows me to run the macro to
export all the data to a text file, How can I include a code into this to
export the data to a text file when I press the save button on excel.

The code I have is below.

Regrads

Sub WriteFixed()

Const MyPath = "C:\documents and settings\Mark\desktop\"
Const WriteFileName = "Trial1.txt"

Const ForReading = 1, ForWriting = 2, ForAppending = 3

Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Set fswrite = CreateObject("Scripting.FileSystemObject")

'open files
WritePathName = MyPath + WriteFileName
fswrite.CreateTextFile WritePathName
Set fwrite = fswrite.GetFile(WritePathName)
Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault)

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For RowCount = 1 To LastRow
LastCol = Cells(RowCount, Columns.Count).End(xlToLeft).Column
OutPutLine = ""
For Colcount = 1 To LastCol
Data = Trim(Cells(RowCount, Colcount).Text)
If Len(Data) < 12 Then
Data = Data & WorksheetFunction.Rept(" ", 12 - Len(Data))
Else
Data = Left(Data, 12)
End If
If OutPutLine <> "" Then
OutPutLine = OutPutLine & " "
End If
OutPutLine = OutPutLine & Data
Next Colcount
tswrite.writeline OutPutLine
Next RowCount

tswrite.Close

End Sub


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB macro code does not save Mary G Microsoft Excel Programming 1 17th Aug 2008 08:34 AM
Code for running macro on save santaviga Microsoft Excel Programming 0 23rd Jan 2008 01:28 AM
save macro code =?Utf-8?B?UmV0YWluaW5nIGF1dG8gZGF0ZSB2YWx1ZXM=?= Microsoft Excel Crashes 1 4th May 2007 05:11 PM
Macro Save As Code??? Chris Watson Microsoft Excel Worksheet Functions 2 28th Jan 2006 10:40 AM
Macro VBA code to name Save-As file =?Utf-8?B?RG9sb3Jlcw==?= Microsoft Excel Worksheet Functions 7 28th Dec 2005 11:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:21 AM.