vb macro reading from txt file in excel

Posted on 2014-03-16
Last Modified: 2014-03-20

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

Question by:rutgermons
LVL 45

Expert Comment

ID: 39933138

* 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.
LVL 23

Accepted Solution

Michael74 earned 500 total points
ID: 39933346
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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question