ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Load text file and write back (https://www.excelbanter.com/excel-programming/299389-load-text-file-write-back.html)

sanjay

Load text file and write back
 
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

--
Message posted from http://www.ExcelForum.com


Dave Peterson[_3_]

Load text file and write back
 
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



"sanjay <" wrote:

Hi,

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

Regards,

Sanjay

---
Message posted from http://www.ExcelForum.com/


--

Dave Peterson



All times are GMT +1. The time now is 12:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com