I have a code to log usage in an excel sheet, but I get one bug, and one issue

后端 未结 2 992
小鲜肉
小鲜肉 2021-01-26 19:02

This is a universal log system, that a few people here and myself have created. I\'m rather proud of it... I am running into two issues... if someone can help with the sollution

2条回答
  •  别那么骄傲
    2021-01-26 19:54

    This worked for me. Ideally you'd have a named range on the sheet being tracked which you could use to restrict tracking only to changes occuring inside that range.

    Const MAX_TRACKED_CELLS As Long = 50
    Dim PreviousValues As Object
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim c As Range
        Dim haveDict As Boolean, val, addr
    
        haveDict = Not PreviousValues Is Nothing
    
        If Target.Cells.Count <= MAX_TRACKED_CELLS Then
            For Each c In Target.Cells
                addr = c.Address()
                If haveDict Then
                    If PreviousValues.exists(addr) Then
                        val = PreviousValues(addr)
                    End If
                Else
                    val = "{unknown}"
                End If
    
                If c.Value <> val Then
                    Debug.Print "Changed:", addr, IIf(val = "", "Empty", val), _
                                " to ", IIf(c.Value = "", "Empty", c.Value)
                End If
    
            Next c
        End If
    
    
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim c As Range
    
        If PreviousValues Is Nothing Then
            Set PreviousValues = CreateObject("scripting.dictionary")
        Else
            PreviousValues.RemoveAll
        End If
    
        If Target.Cells.Count <= MAX_TRACKED_CELLS Then
            For Each c In Target.Cells
                PreviousValues.Add c.Address(), c.Value
            Next c
        End If
    
    End Sub
    

提交回复
热议问题