Solved

Excel VBA - Write from worksheet to text file

Posted on 2013-01-12
4
482 Views
Last Modified: 2013-01-12
Hi,

I woud be very grateful if someone could help me with a task that I have not done before, and hence not aware of a good approach I should adopt.

I have an Excel workbook that is populated with a list of files from a folder that I would like to output to a text file.  I attach a sample workbook:

1.  There are 3 columns and n number of rows.

2.  The first row contains the text "Filename" in the 1st column, a date in the 2nd column and a date in the 3rd column.  This row needs to appear in the first line of the text file with a tab separating the 3 data values.

3.   The data rows beginning at row 2 in the worksheet should be output to separate lines in the text file.

4.   A blank row should appear in the text file after the last data row.

5.   The text file should be saved to the same folder in which the worbook has been saved and should be called "DataOutput.txt".

If it helps I also attach a "DataOutput.txt" that I have prepared manually.


Many thanks in advance
Alison
OutputToFile-test.xlsm
DataOutput.txt
0
Comment
Question by:alisonthom
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
als315 earned 177 total points
ID: 38770338
Look at sample
OutputToFile-test.xlsm
0
 
LVL 46

Assisted Solution

by:Martin Liss
Martin Liss earned 178 total points
ID: 38770352
Here's a macro
Sub CreateFile()
Dim lngLastRow As Long
Dim lngIndex As Long
Dim strLine As String

lngLastRow = Range("A65536").End(xlUp).Row
Open Application.Path & "\DataOutput.txt" For Output As 1
Print #1, Cells(1, 1).Value & vbTab & Cells(1, 2).Value & vbTab & Cells(1, 3).Value
For lngIndex = 2 To lngLastRow
    Print #1, Cells(lngIndex, 1).Value
Next
Close

End Sub

Open in new window

0
 

Author Closing Comment

by:alisonthom
ID: 38770552
Thank you both for providing excellent solutions!

It is most appeciated
Alison
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 38770573
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

821 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