Appending to VBA log file on first line and not the last line

vba write to text file without quotes
vba write to text file new line
vba write to text file append
vba read text file line by line
copy data from excel to text file using vba
vba read text file into array
vba open text file in notepad
excel vba find and replace string in text file

I have written the below code to create a log file and it works fine. However, I wanted the last log entry on the 1st line rather than the last line.

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

Dim logFile As Object
Dim logFilePath As String
Dim logFileName As String

'Replace 'TestLog' with your desired file name
logFileName = "SABRE_RET_LOGFILE" & ".log"
myFilePath = "C:\Users\Sac\Documents\SS\SABRE\" & logFileName 'Modify the path here

If fso.FileExists(myFilePath) Then
    Set logFile = fso.OpenTextFile(myFilePath, 8)
    ' create the file instead
    Set logFile = fso.CreateTextFile(myFilePath, True)
End If

logFile.WriteLine FileName & " was opened using " & ThisWorkbook.Name & " opened by " & Application.UserName & " " & Format(Now, "yyyy-mm-dd hh:mm")

logFile.Close ' close the file

This might not be satisfying answer to your question, but have you considered using a log viewer app? Most will allow you to filter and sort data.

Otherwise, there's no efficient way to do what you want to do. You'd have to .ReadAll and re-write it every time AFAIK.

VBA, then create a second file, open it and read the recs in first file, writing second either changed or unchanged as needed. and adding any at end. Appending an existing text file Using VBA Here is the Procedure, Example VBA Syntax and Example VBA Macro code for Appending an existing text file in Excel VBA. This will help you to know how to append a text file and add data using VBA. VBA append to existing text file: Procedure We will first open the text file as Append file with a file number.

Not sure this is the answer you seek, but you could just write to the file, but have a think about structuring it, into a delimited file, then rather than open the file query it using ADO and the SQL ordered by the date descending.

The VBA Guide To Interacting With Text (.Txt) Files — The , I like to use them to save settings for my VBA add-ins and I have seen situations You will not be able to modify the text file while opening it with this mode. Write - This writes a line of text to the file surrounding it with quotations However, LAST MODIFIED property was updated for testfile.txt but was not� In the simplest case, messages are written to a single log file. Consider a scenario, the application is logging the user details into the log file who are accessing the application. Log files are useful in different situations, especially for developers. Log files are plain text files that can store information temporary or more permanent.

Many thanks for all the responses, much appreciated!

I thought it would be something easy but based on your response, it turned quite tricky. Hence, I am working with the log file as it is created.

The whole purpose I wanted the log to be created was to validate the last accesses file. So, I am currently looping through all the logs in the log file.

And, it is working fine.

Once, again thanks for all the help!

Visual Basic for Applications/Use Log Files from VBA, next line of a log file 'You cannot overwrite this file; only append or read. 'If path parameter not given for file, or does not exist, defaults are used. 'Needs a VBA� In this article I will explain how you can append data to a text file.I am assuming readers are familiar with modifying text files.If you are not familiar with modifying text files I’d recommend taking a look at the this article first VBA, Modify Existing Text File.

Write To A Text File, In this lesson, you'll learn how to write data from a spreadsheet to a text file. However, suppose we decided to add more rows, or more columns to the Row. This code first goes to the very last row in Excel, no matter which version you have� VBA Code to Append to Text File. The whole trick lies in to the mode you open your text file during your VBA program. Why I say that, because you do not need to change anything else in the Write or Print statements in order to append and not to replace. Isn’t it simple?

How to: Open and append to a log file, Open and append to a log file using the StreamWriter and StreamReader classes in . Visual Basic users may choose to use the methods and properties provided WriteLine(line); } } } // The example creates a file named "log.txt" and Is this page helpful? Yes No. Any additional feedback? Skip Submit. Would someone please suggest VBA code for inserting one line after each row in a file? I have a file with 4064 lines, I'd like a line between all 4064 of them. Right now, I can only insert line between fixed rows, each chosen in advance: Sub insert() ' insert a line Rows("4065:4088").Select

Reading txt file from the last line in vb6, Hi, Is there any option available to read a text file from its end. The number of characters / lines to be read differs for each run. Actually I have to read the log file � Here is a bit of useful code when having to write text to a log file. It could be at points through a long process where the code may stop, having a log at where it stopped is important.