Hi,
Excel 2k
I have written a program in excel that saves information from various cells:
John 7/24/2004 14 3 1 0 00:16:42 00:59:28 05:00:32
to a flat file called UserStat.log on my C:\Logcall\ directory
On any given day, there maybe more that one line in that flat file:
John 7/24/2004 14 3 1 0 00:16:42 00:59:28 05:00:32
patrick 7/24/2004 14 3 1 0 00:16:42 00:59:28 05:00:32
gilles 7/24/2004 14 3 1 0 00:16:42 00:59:28 05:00:32
I want to be able to update any information for a particular line when the user saves
his/her work back to the userstats.log file without deleting and any of the
line either bellow or above the one in question. Say, I want to update the value '14' for 'Patrick', which is
line 2, column 3.
What I have written is too confusing and does not accomplish the task. I wanted to loop through the flat
file and look for a paricular name. once the name is found, then update any of that information on that line only. I am able to
loop through the flat file and find the name, but i don't know how to upate the information or delete the entire line and replace it with the newly updated info from the user when the work is saved.
Can you help me?
Here is the code I wrote:
To save the info on the flat file
Sub WriteUserStats()
Dim FileName As String
Dim strLine As String
Dim intFn As Integer
Dim strOutFile As String
intFn = FreeFile
strOutFile = "C:\LOGCALL\UserStat.log"
Open strOutFile For Append As #intFn
strLine = Range("C1").Value & " " & Date
strLine = strLine & " " & Range("C4").Value & " " _
& Range("d4").Value & " " _
& Range("e4").Value & " " _
& Range("f4").Value & " " _
& Range("g4").Value & " " _
& Format(Range("h4").Value, "HH:MM

S") & " " _
& Format(Range("i4").Value, "HH:MM

S") & " " _
& Format(Range("J4").Value, "HH:MM

S")
Print #intFn, strLine
Close #intFn
End Sub
Then to find the name to update:
Private Sub FindNameToUpdate()
Dim c As Long
Dim hFile As Long
Dim strLine As String
Dim FileName As String
Dim TextLIne As Variant
Dim sTableDetails As String
Dim RepName As String
Close #hFile
hFile = FreeFile
FileName = "C:\LOGCALL\UserStat.log"
Open FileName For Input As #hFile
Close #hFile
Open FileName For Input As #hFile
Line Input #1, TextLIne
Do While Not EOF(1)
Line Input #1, TextLIne
Dim sp1, strlen As Integer
Dim one, two As String
sp1 = InStr(1, TextLIne, " ")
one = Left(TextLIne, sp1 - 1)
If one = "PATRICK" Then
one = "ALEXANDER"
strLine = one
End If
Loop
Close #hFile
WriteUserStats (strLine)
End Sub
Then to update the values for a particular line????
Function WriteUserStats(myvar As String)
Dim FileName As String
Dim strLine As String
Dim intFn As Integer
Dim strOutFile As String
Dim TextLIne As Variant
intFn = FreeFile 'grab next available DOS file handle
strOutFile = "C:\LOGCALL\UserStat.log"
Open strOutFile For Append As #intFn
'Print #intFn, myvar
MsgBox myvar
'************************************************* *********
' this part was to replace the value that matches the name
'but that does not work.
'************************************************* *********
Do While Not EOF(1)
Dim sp1 As Integer
Dim one As String
sp1 = InStr(1, myvar, " ")
one = Left(myvar, sp1 - 1)
If one = myvar Then
strLine = myvar
Print #intFn, strLine
End If
Loop
'************************************************* *********
Close #intFn
End Function