Load text file and write back

S

sanjay

Hi,

Is it possible for me to read a text file (just contains a number) an
write the number to a cell and then increment that number and save tha
back into the text file?

Regards,

Sanja
 
D

Dave Peterson

It's possible, but you don't need to do that much work.

Option Explicit
Sub testme01()

Dim myNumber As Long
Dim myLine As String
Dim myFileName As String
Dim FileNum As Long

myFileName = "C:\my documents\excel\text.txt"
FileNum = FreeFile

'read previous number:
If Dir(myFileName) = "" Then 'not found
MsgBox "File is missing!"
Exit Sub
Else
Close FileNum
Open myFileName For Input As FileNum
Do While Not EOF(FileNum)
Line Input #FileNum, myLine
myNumber = Val(myLine)
Loop
Close FileNum
End If

myNumber = myNumber + 1

Open myFileName For Output As FileNum
Print #FileNum, myNumber
Close FileNum

End Sub
 
Top