edit textfile error

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I have this code in my workbook open and workbook close events. When the workbook opens, the code is to edit the second entry (which it does exactly what it is suppose to do), however, when I close the workbook, instead of editing the second entry, it enters duplicate entries. The code is only to edit the second entry of the text file.

The code is to only edit the second entry only, not create duplicates. Can someone help fix this problem.

43678.3604398148
43691.4231597222



Code:
    Set fs = CreateObject("Scripting.FileSystemObject")    
    f = fs.OpenTextFile(OPath & TmEvnt).readall
    pTime = Trim(Split(f, vbCrLf)(0))
    CurrentTime = Format(Now, "#0.#########0")
    
        Open OPath & TmEvnt For Input As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        Do While Not EOF(1)
            Line Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , s
            n = n + 1
            If n = 2 Then s = CurrentTime
            ss = ss & s & vbNewLine
        Loop
        Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        
        Open OPath & TmEvnt For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , ss
        Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]
 
" the file should not have no more then two lines"

If you only want two lines to be output...

remove the loop, why read the entire file when youre only interested in the first two records?
just do a

Open the file for input
Read into variable 1
Read into variable 2
Close the file

Open the file for output
Write variable 1
Write variable 2
Close the file
 
Last edited:
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I found my problem accidentally when I discover my Dim variables were place at the top of all the code instead of placing them in each of the Workbook Open event and in the Workbook Closed Event. Now it works as expected. Thank you , I greatly appreciated this forum for all the help.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,031
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top