Solved

Excel VBA - Write from worksheet to text file

Posted on 2013-01-12
4
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

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

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 47

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

739 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