Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2008

Tip: Looking for answers? Try searching our database.

VBA for Exporting to Text file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
santaviga - 18 Jan 2008 16:38 GMT
I have input the following code and it works to an extent, when it runs
the macro to export the data to text file, the text is all joined and not in
columns, is there anywhere I need to put in so if there is only 5 characters
it will pad the text out to 12 and therefore keep the text file as if it were
in columns?

Many thanks

> Sub WriteFixed()
>
> Const MyPath = "C:\temp\"
> Const WriteFileName = "text.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 = Cells(RowCount, Colcount).Text
>        If Len(Data) < 12 Then
>           Data = Data & WorksheetFunction.Rept(" ", 12 - Len(Data))
>        End If
>        OutPutLine = OutPutLine & Data
>       Next Colcount
>       tswrite.writeline OutPutLine
>    Next RowCount
>      
>    tswrite.Close
>    
> End Sub
>
> "santaviga" wrote:
>
> > Hi to all.
> >
> > I need some help with exporting excel data to text file, I have data in
> > cells throughout the worksheet, what I need is a macro to export the data in
> > all cells to a text file on the desktop, but these need to be a maximum of 12
> > characters and if not 12 character it needs to be padded out to 12 characters
> > so that when it exports to a text file the data is displayed as if in colums
> > and not all over the place.
> >
> > Also I need to know how it exports e.g every time I save the file will it
> > export to a text file on desktop?
> >
> > I am currenty doing this a long way by formulas but I think would be better
> > if it was done by a macro.
> >
> > I thankyou in advance
> >
> > Any help would be much appreciated I am new to macros
> >
> > Regards
ryguy7272 - 18 Jan 2008 16:50 GMT
I use this simple macro:
Sub SaveAsText()
ThisWorkbook.SaveAs Filename:="MyText.txt", _
FileFormat:=xlTextWindows
End Sub

Regards,
Ryan--

Signature

RyGuy

> I have input the following code and it works to an extent, when it runs
> the macro to export the data to text file, the text is all joined and not in
[quoted text clipped - 62 lines]
> > >
> > > Regards
santaviga - 18 Jan 2008 17:36 GMT
Hi Ryan, That code you gave me not doing anything I need, its creating the
text file but nothing in it and clearing data in wxcel.

> I use this simple macro:
> Sub SaveAsText()
[quoted text clipped - 71 lines]
> > > >
> > > > Regards
ryguy7272 - 18 Jan 2008 19:42 GMT
Hummm, not sure why it is behaving like this; it works fine for me.  Did you
check 'My Documents' for the file?  I just tested it again; works fine here.

Ryan--

Signature

RyGuy

> Hi Ryan, That code you gave me not doing anything I need, its creating the
> text file but nothing in it and clearing data in wxcel.
[quoted text clipped - 74 lines]
> > > > >
> > > > > Regards
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.