Solved

vb macro reading from txt file in excel

Posted on 2014-03-16
2
774 Views
Last Modified: 2014-03-20
Folks

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)
Loop

' Close the file
Close
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

cheers
output.txt
Output-Reader.xlsx
0
Comment
Question by:rutgermons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 39933138
@rutgermons

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

Accepted Solution

by:
Michael Fowler 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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

691 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