vb macro reading from txt file in excel


I have a macro that reads data from a log file into excel but I want it to only add records
to the excel sheet rows ontop the records that already exist in the sheet, how can i ensure only new records get added if they are unique

below is my current code which merely overwrites, I have attached the txt file and the excel sheet for test purposes

Sub ReadFile()
Dim FF As Integer
Dim strData As String
Dim strParts() As String
Dim lngRow As Long
' A one-line heading is assumed.
lngRow = 1

' Get the first unused file number
FF = FreeFile

' Open the file for reading
Open "C:\temp\output.txt" For Input As #FF

' Read the reacords until end of file
Do While Not EOF(FF)
    lngRow = lngRow + 1
    ' Read one line at a time into strData
    Line Input #FF, strData
    ' Split the data at the comma
    strParts = Split(strData, ",")
    ' Place the data in columns A to B.
    Cells(lngRow, 1) = strParts(0)
    Cells(lngRow, 2) = strParts(1)

' Close the file
End Sub

fyi, the output file is a text file that goes and adds entries on a periodic basis hence me wanting to add only new entries

Who is Participating?

Improve company productivity with a Business Account.Sign Up

Michael FowlerConnect With a Mentor Solutions ConsultantCommented:
Your code appears to place the log files into excel in date order ascending. Is this correct?

I would read the last entry of the current excel sheet to get the date.
Then read through the log file until you get to line where the datetime in the log is greater than or equal to the last excel entry AND the message in the log is different to the message in excel.
Once this is true you can start adding the lines to excel.

If you are trying to add entries at the top of the file you will need to insert a line before adding the value

* Your imported example does not match the worksheet contents.
* Your sample files do not seem to show the duplicates scenario you described, since they are entirely duplicates.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.