Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write array to text file without loop

Status
Not open for further replies.

FractalWalk

Technical User
Nov 18, 2002
141
US
Using VBA within Excel 2007, I am writing a 1-deminsional array to a text file by opening a file for output, looping through each element of the array and printing them to the file.

Open (path of file) For Output As #1
For a = 0 To b
Print #1, Data(a)
Next a
Close

It works but it is very slow. Is there a way to write the entire array to the text file all at once?

 
Hi,

Where is the data for your array coming from?

I suspect that you're grabbing it from data on a sheet. Depending on the format & logic, it could be as simple as a SaveAs to a text file, sans any VBA code.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The data does not exist separately from the subroutine logic. In other words, the data is read in from a file, modified within the code and then printed back to a file.

I guess I could write the array to the spreadsheet and then save it to a text file.
 


Could you use Data > Import External Data > Import to get the data in a sheet?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 

Reading in the data is not a problem. That is very quick directly from the source file. It is the writing of the data back to an output text file once it has been modified that is the issue.

I can read in the data and alter it with my logic and store it in an array all very quickly. But then I need to write it out to a text file and that is very slow when I have to go line by line.

It would be easy and quick to drop the array onto a spreadsheet (Range("XX:XX")= MyArray). And then maybe I could just save the spreadsheet as a text file.
 
Try:

[tt]Print #1, Join(Data, vbCrLf)[/tt]

or perhaps assign to a variable before printing?

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top