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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append range to text file

Status
Not open for further replies.

ChrisBurch

IS-IT--Management
Jul 3, 2001
184
AU
I would like to take a row of data from Excel 97, and append it to an existing text file. Anyone got any clues?

I can successfully take the entire sheet and overwrite the text file, but as the file gets larger it will take longer & longer. Hence the attempt to append just the new data. Chris

It worked yesterday.
It doesn't work today.
That's Windows!
 
Hi Chris,

It should more or less work as follows:

Code:
Sub Test()
  Dim iFile as Integer
  Dim str as string
  
  iFile = Freefile()

  Open "MyFile.txt" for Append as #iFile
  str ="New Info I want to append"
  Print #iFile, str
  
  Close "#iFile
End Sub
[\code]

Nath
 
Thanks Nath,
Wasn't quite what I was looking for, as I was trying to get a delimited output, but gave me a starting point. I ended up using 'Write" instead of 'print', & putting my ranges into variables. The date output is peculiar
( #2001/12/11# ), but I'll fix this in Excel.

'Output selected cells in comma delimited format
'and Append to existing text file

Private Sub CommandButton1_Click()
Application.DisplayAlerts = False
ActiveWorkbook.Save

Dim iFile As Integer
Dim myDate As Date
Dim myJob As String
Dim myRolls As Integer
Dim myPallet As Integer

ActiveWorkbook.Sheets("sheet1").Select
myDate = ActiveWorkbook.Sheets("Sheet1").Range("F25")
myJob = ActiveWorkbook.Sheets("Sheet1").Range("G25")
myRolls = ActiveWorkbook.Sheets("Sheet1").Range("H25")
myPallet = ActiveWorkbook.Sheets("Sheet1").Range("I25")

iFile = FreeFile()

Open "c:\windows\desktop\test.txt" For Append As #iFile

'comma delimited output
Write #iFile, myDate, myJob, myRolls, myPallet

Close #iFile

End Sub

There's probably better ways of doing it, but this works for me until my knowledge improves. Chris

It worked yesterday.
It doesn't work today.
That's Windows!
 
Hi,
From an old mainframer, hre's what SOP is:
Open File1 For Sequential Input
Open File2 for Output
Do Until EOF1
Read File1 Into Buffer
If Condition Then
Write File2 From Other
Else
Write File2 From Buffer
Loop
Close File2
Close File1
If (the process ended without error) Then
Delete File1
Rename File2 As File1
End

SOP!

Skip,
metzgsk@voughtaircraft.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top