logo ASAP Utilities

Excel tip: Write data to a textfile

Date: 5 september 2001

The following code will write data to a text file. If the file doesn't exist yet, it will be automaticly created. The original text will be replaced every time the code is run.


Copy-paste friendly code:
Sub WriteToTextFile()
' writes data to a text file
' if the file does not exists, it will be created.
      Dim iFileNumber                        As Long
      Dim strData                            As String
      Dim strFileName                        As String

      iFileNumber = FreeFile()
      strData = "Test data"            'the text written in the file
      strFileName = "C:\test.txt"      ' the text file
      Open strFileName For Output As #iFileNumber
      Print #iFileNumber, strData
      Close #iFileNumber
End Sub



« back

Home Privacy Policy Cookie Policy EULA Download All added Excel tools Sitemap Contact Us


🏆 Winner of the "Best Microsoft Excel Add-in" Award at the Global Excel Summit 2025 in London!
For over 26 years, ASAP Utilities has helped users worldwide save time and do what Excel alone cannot.