LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Export Data to Text file with fix format

On Jul 21, 7:57*pm, Rushna wrote:
On Jul 21, 12:20*am, "Rick Rothstein \(MVP - VB\)"





wrote:
Your macro exported only one record i.e the last record as follows.
5894 * * 003000096215.03.2015.03.20000000000049184USD *AABBCCDDXXX


Please note the following errors
1) Col H and I - Date is exported as 15.03.20 (please note that the
year is 2006) - it should be displayed as 20060315 in both case.
2) Col K (value is -49,183.80) the output is displayed as
000000000049184 - it should be displayed as 000000004318380 (i.e
leading zeros, two decimal places without any decimal signs)


Argh! The single record print out was because I specified the wrong variable
in the Print statement. The other errors were because of my misreading what
you wrote. Give this revised code a try...


Sub WriteDataOut()
* Dim X As Long
* Dim FF As Long
* Dim LastRow As Long
* Dim Dte As String
* Dim Record As String
* Dim TotalFile As String
* Dim FileNameAndPath As String
* FileNameAndPath = "c:\Dir1\Dir2\etc\FileName.txt"
* With Worksheets("Sheet1")
* * LastRow = .Cells(Rows.Count, "C").End(xlUp).Row
* * For X = 10 To LastRow
* * * Record = Space$(66)
* * * Mid$(Record, 1) = .Cells(X, "C").Value
* * * Mid$(Record, 10) = Format$(.Cells(X, "F").Value, "0000000000")
* * * Dte = .Cells(X, "H").Value
* * * Mid$(Record, 20) = Right$(Dte, 4) & Mid$(Dte, 4, 2) & Left$(Dte, 2)
* * * Dte = .Cells(X, "H").Value
* * * Mid$(Record, 28) = Right$(Dte, 4) & Mid$(Dte, 4, 2) & Left$(Dte, 2)
* * * Mid$(Record, 36) = Format$(100 * Abs(.Cells(X, "K").Value), _
* * * * * * * * * * * * * * * * * * * * * * * "000000000000000")
* * * Mid$(Record, 51) = .Cells(X, "L").Value
* * * Mid$(Record, 56) = .Cells(X, "Q").Value
* * * TotalFile = TotalFile & vbCrLf & Record
* * Next
* * FF = FreeFile
* * Open FileNameAndPath For Output As #FF
* * Print #FF, TotalFile
* * Close #FF
* End With
End Sub


Rick


Works perfect. Thanks a lot for your help.

Rushna.- Hide quoted text -

- Show quoted text -


Hello All,
The macro below provided by Mr. Rick Rothstein works perfect. It
export the worksheet to a text file. I wish to change it further to
suit to my needs.

Sub WriteDataOut()
Dim X As Long
Dim FF As Long
Dim LastRow As Long
Dim Dte As String
Dim Record As String
Dim TotalFile As String
Dim FileNameAndPath As String
FileNameAndPath = "c:\Dir1\Dir2\etc\FileName.txt"
With Worksheets("Sheet1")
LastRow = .Cells(Rows.Count, "C").End(xlUp).Row
For X = 10 To LastRow
Record = Space$(66)
Mid$(Record, 1) = .Cells(X, "C").Value
Mid$(Record, 10) = Format$(.Cells(X, "F").Value, "0000000000")
Dte = .Cells(X, "H").Value
Mid$(Record, 20) = Right$(Dte, 4) & Mid$(Dte, 4, 2) & Left$(Dte, 2)
Dte = .Cells(X, "H").Value
Mid$(Record, 28) = Right$(Dte, 4) & Mid$(Dte, 4, 2) & Left$(Dte, 2)
Mid$(Record, 36) = Format$(100 * Abs(.Cells(X, "K").Value), _
"000000000000000")
Mid$(Record, 51) = .Cells(X, "L").Value
Mid$(Record, 56) = .Cells(X, "Q").Value
TotalFile = TotalFile & vbCrLf & Record
Next
FF = FreeFile
Open FileNameAndPath For Output As #FF
Print #FF, TotalFile
Close #FF
End With
End Sub



Changes required:
The file should be saved in the given Folder Path using the Cell Value
(E5) with date and time stamped e.g if cell value (E5) in the excel
file is 1234 then the file should be saved as "1234 (date)
(time)".txt . If the value in E5 is 3456 then it should be saved as
"3456 (date) (time).txt"

At present it saves as filename.txt and it overwirtes the previous
version too. I need to have all the old files with date and time
stamped for future references using the Cell Value in E5.

Can someone help me please?

Thanks in advance

Rushna
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I export data in text format with minimum of spaces? [email protected] Excel Discussion (Misc queries) 1 February 12th 07 10:57 PM
How to make text data export to excel in text format. ~@%.com Excel Programming 3 March 21st 06 03:16 AM
Open CSV file, format data and write output to a text file. BristolBloos Excel Programming 1 October 18th 05 03:50 PM
How do I import text file, analyze data, export results, open next file Geoffro Excel Programming 2 March 6th 05 08:02 PM
Convert data and export to a text file Will[_7_] Excel Programming 1 May 11th 04 09:44 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"