update a txt file if dta is new

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In A5 I input a name on a daily basis, I am trying to write a macro that will
take the data in A5 and put it in a new line in a text file located at
c:/temp/list.txt
I will need to check first if the name is already there (there is only one
name per line) and if it is there not to add it again.
Can that be done.
Many thanks
Mark
 
Mark,

Try this:

Sub Append_Line()
Dim vFile As String
Dim vLine As String
Dim vNewLine As String
Dim vFound As Boolean
vFile = "C:\Temp\List.txt"
vNewLine = Range("A5").Value
vFound = False
Open vFile For Input As #1
Do Until EOF(1)
Line Input #1, vLine
If vLine = vNewLine Then
Close #1
Exit Sub
End If
Loop
Close #1
Open vFile For Append As #1
Print #1, vNewLine
Close #1
End Sub

HTH,
Nikos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top