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]
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I'm no VBA expert but looking at that code it will...

1. Read each record in the file and produce an overall total of the amount of seconds in the file, replacing the number of seconds with the current time on the 2nd record
2. Create a new file with the same name, outputting just ONE record with the total amount of seconds that have been calculated.

Is that what you are expecting it to do?

NOTE: That Currenttime is OUTSIDE the loop so if the time now is 15:00:00 and it takes 5 minutes to read the file the current time will still be 15:00:00 not 15:05:00
 
Last edited:
Upvote 0
When I first start my workbook for the very first time, it records two times.

Start time
Current Time

The Start Time should never be change, however, I want to record the the Current Time when the workbook opens or closes. When it opens, it does, exactly that; Only edits the Current Time. However, when I close the workbook, for some reason it creates duplicate entries of the Start Time and the Current time. (basically, 4 lines).
 
Upvote 0
Oops I mistook & for +

Youre not adding time, you're concatenating a string of times separated by a newline

Its cos of this

Code:
ss = ss & s & vbNewLine

whatever ss is already
s & newline are added to the string.

Then when the next record is read that result becomes longer since youre adding s and newline to an already existing value of ss

Like

AA
AA BB
AA BB CC
AA BB CC DD

separated by a newline
 
Upvote 0
Cross posted http://www.vbaexpress.com/forum/showthread.php?65679-Edit-textfile-vba

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
When I open the workbook, the code edits the second line in the file

43678.3604398148
43691.4901387963

however, when I close the workbook and check the text file, this is the output.

43678.3604398148
43691.4901388889
43678.3604398148
43691.4901388889

I don't want it to make duplicate entry. I only want to edit the red line above with the current time. the file should not have no more then two lines. The only line the should be edited is the second line (red line) . in end, the second line is being replace with the current time.
 
Last edited:
Upvote 0
My apologies Fluff. I'm at my witts end trying to fix this problem. So I thought I would get extra help . Please except my apologies.

Cross posted http://www.vbaexpress.com/forum/showthread.php?65679-Edit-textfile-vba

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Please just remember to supply links in the future.
 
Upvote 0
Is it still possible to get an answer to my problem. Really appreciate the help.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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