Consulting

Results 1 to 6 of 6

Thread: How to Log Changes on Sheets!

  1. #1

    How to Log Changes on Sheets!

    I have spreadsheet. I am trying to write a code so that when one specific cell is changed to a number greater than 0 it will record the date and time of this occurrence on another sheet. Then if this cell is changed again to another number greater than 0 I want it to record that date in an ever expanding list. So that every time a number is recorded in that cell it adds to a list on another sheet to capture every time data was entered in that cell that was greater than 0. I would be fine if we also did this as a VBA code that we assign as a macro to a button that opens a text prompt that asks for the number for that cell and when you enter the number and confirm it in the prompt as long as it is greater than 0 it then adds the date of the occurrence to the list in the other sheet. Let me know if you have any questions or concerns.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. Welcome to the forum -- please take a minute and read the FAQs in my sig

    2. A more descriptive title that "Help" works better

    3. This goes in the worksheet code module, not a standard Module1, ....


    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rMagicCell As Range, rLogCell As Range
    
    
        Set rMagicCell = Range("B4")    '   <<<<<<<<<<<<<<<<<<<<<< change
        
        If Intersect(rMagicCell, Target.Cells(1, 1)) Is Nothing Then Exit Sub
    
    
        If rMagicCell.Value <= 0# Then Exit Sub
    
    
        With Worksheets("Log")
            Set rLogCell = .Cells(.Rows.Count, 1).End(xlUp)
            
            If Len(rLogCell.Value) > 0 Then Set rLogCell = rLogCell.Offset(1, 0)
        End With
        
        rLogCell.Value = Now
    
    End Sub
    I didn't understand the MsgBox part of your question
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Thanks Paul. That works perfectly. One more quick question. If I wanted to also do the same thing in the same worksheet, but with B6 also but put it to Worksheets("Log2") how would I do that?

    P.s. I will make sure to follow the rules. I apologize for my poor post. I just have been trying to get this code to work for two days. I was just as frustrated as could be. I can't believe you got it working that fast. And you even guessed exactly what I was doing... Putting it in a standard module.
    Last edited by nick11medic; 10-18-2019 at 10:14 AM.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    An idea inspired by Paul's Excellent code

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
       If Not Intersect(Range("B4"), Target) Is Nothing Then LogB4 Range("B4")  'Edit B4 as needed
       If Not Intersect(Range("B6"), Target) Is Nothing Then LogB6 Range("B6")
    End Sub
    
    Private Sub LogB4(ByVal Target As Range) 'Change Name "LogB4" as needed
       If Target = 0 Then Exit Sub
       Worksheets("Log").Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0) = Now
    End Sub
        
    Private Sub LogB6(ByVal Target As Range)
       If Target = 0 Then Exit Sub
       Worksheets("Log2").Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0) = Now
    End Sub
    My way:
    Private Sub logSamT(ByVal Target As Range)
    'Records time of change, what cell was changed, who changed it and the new value
    'To use in worksheet Change sub:
    '  If Not Intersect(Range("B4"), Target) Is Nothing Then LogSamT Range("B4")
    '  If Not Intersect(Range("B6"), Target) Is Nothing Then LogSamT Range("B6")
    'Repeat for each desired range
    
    Dim NewCell As Range
    
       With Worksheets("logSamT")
          Set NewCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
          NewCell = Now
          NewCell.Offset(0, 1) = Target.Address
          NewCell.Offset(0, 2) = Application.UserName
          NewCell.Offset(0, 3) = Target.Value
       End With
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Showing different techniques

    Some techniques can more easily be expanded when requirements change, or bugs need to be squashed



    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rLogCell As Range
        Dim wsLog As Worksheet
        
        Select Case Target.Cells(1, 1).Address
            Case "$B$4"
                Set wsLog = Worksheets("Log")
            Case "$B$6"
                Set wsLog = Worksheets("Log2")
            Case Else
                Exit Sub
        End Select
    
    
        If Target.Cells(1, 1).Value <= 0# Then Exit Sub
    
    
        With wsLog
            Set rLogCell = .Cells(.Rows.Count, 1).End(xlUp)
            
            If Len(rLogCell.Value) > 0 Then Set rLogCell = rLogCell.Offset(1, 0)
        End With
        
        rLogCell.Value = Now
    
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Y'all are amazing. I just need to sit down with y'all one day and learn how to code better in excel.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •