Vb-how To Insert Header In Logfile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smugcool
    New Member
    • Apr 2007
    • 81

    #1

    Vb-how To Insert Header In Logfile

    Hi all,

    Below are the codes which i wrote to monitor changes in excel. Now i wanted to add Header for the text file which will be generated. LIke suppose if change is happend in the row. i wanted to give name of the hader as "ROW". In the text file.

    if changes are happening in the column i wanted to Name it as column.Kindly help me??

    below are the codes :-
    [code=vb]
    Private Sub Workbook_SheetC hange(ByVal Sh As Object, ByVal Target As Range)
    Dim W As Worksheet

    Set W = Sh

    Dim C As Range

    Dim Fnum As Long
    Dim strFileName As String
    strFileName = "C:\Documen ts and Settings\Anup\D esktop\book1.tx t" & Format(Now(), "yyyy-mm-dd" & ".Txt")
    Fnum = FreeFile
    Open strFileName For Append Access Read Write Lock Write As #Fnum
    For Each C In Target.Cells
    With C
    'Debug.Print , W.Name, .Column, .Row, .Value, .Value2,.Applic ation.UserName
    Print #Fnum, W.Name; vbTab; .Column; vbTab; .Row; vbTab; .Value; vbTab; Application.Use rName; vbTab; Now(); vbTab
    End With
    Next

    Set W = Nothing

    Set C = Nothing

    Close #Fnum

    End Sub
    [/code]
  • danp129
    Recognized Expert Contributor
    • Jul 2006
    • 323

    #2
    I'm not sure I understand. Every cell that's edited is going to have a Row and a Column that it exists in, so how do you plan to determine if it's a row edit instead of a column edit?

    Comment

    • smugcool
      New Member
      • Apr 2007
      • 81

      #3
      go thru the below example:-

      this is the content of the file which is actually generated after change is made

      AUDIT_PERSONALI ZATION 1 104 5/21/2007 b 5/22/2007 5:50:01 PM
      AUDIT_PERSONALI ZATION 2 104 1 b 5/22/2007 5:50:01 PM

      now i want this file to come up with header e.g

      sheet name row column value date username
      AUDIT_PERSONALI ZATION 1 104 5/21/2007 b 5/22/2007 5:50:01 PM
      AUDIT_PERSONALI ZATION 2 104 1 b 5/22/2007 5:50:01 PM

      Is there anyway??

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Why not just use Notepad or something, and create the file with your headers there already. Then your code can just append to the file.

        Or if your code creates the file, it can simply write out the header line before writing the first line of "log info".

        If you need to check whether there's anything in the file yet, after you open it you can use the Lof() function to get the current length (zero if it's empty).

        Comment

        • smugcool
          New Member
          • Apr 2007
          • 81

          #5
          Originally posted by Killer42
          Why not just use Notepad or something, and create the file with your headers there already. Then your code can just append to the file.

          Or if your code creates the file, it can simply write out the header line before writing the first line of "log info".

          If you need to check whether there's anything in the file yet, after you open it you can use the Lof() function to get the current length (zero if it's empty).

          I can't use a format of file since this reports are generated on daily basis.

          i think your second idea is correct. Can u plz guide me the code for writing the header line.As i am not aware of the code.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Well, I don't know your circumstances very well, but it should be pretty simple, something like...

            [CODE=vb]' After opening the file for Append...
            If Lof(FileNum) = 0 Then
            Print #FileNum, "sheet name row column value date username"
            End If[/CODE]

            Comment

            Working...