Excel Guides

Appending to a Non-Excel Text File in Excel

If you have a text file that you want to append data to without opening it in Excel, there are a few ways to do this. One way is to use the FileSystemObject object in Microsoft Excel. This object allows you to work with files and folders on your computer from within Excel. To use this method, you first need to add a reference to the Microsoft Scripting Runtime library in Excel. You can do this by going to Tools > References in the Visual Basic Editor and selecting "Microsoft Scripting Runtime" from the list of available references.

Once you have added a reference to the Microsoft Scripting Runtime library, you can use the following code to append data to a text file:

Dim fso As New FileSystemObject
Dim ts As TextStream
Set ts = fso.OpenTextFile("c:\test.txt", ForAppending)
ts.WriteLine "This is a test."
ts.Close
Set ts = Nothing
Set fso = Nothing

This code will open the text file "c:\test.txt" and append the line "This is a test." to the end of the file. You can change the path and filename to match the location of your text file, and you can change the text that is being appended to the file.

Another way to append data to a text file without opening it in Excel is to use the FileOpen function in Microsoft Visual Basic for Applications (VBA). The FileOpen function allows you to open a file and specify what mode you want to open it in. The mode argument for appending data to a text file is 8. The following code shows how to use the FileOpen function to append data to a text file:

Dim intFileNum As Integer
intFileNum = FreeFile() 'Get next free file number.
FileOpen(intFileNum, "c:\test.txt", OpenMode.Append) 'Open file for appending. 
Print(intFileNum, "This is a test.") 'Write line of text. 
FileClose(intFileNum) 'Close file.

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.