Solved

Excel VBA - Write from worksheet to text file

Posted on 2013-01-12
4
477 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 39

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

776 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