简体   繁体   English

Excel VBA-仅在更改单元格时插入用户名

[英]Excel VBA - Insert Username ONLY when cell is changed

Here's my problem: I have working code to insert a username and timestamp when a user makes a change anywhere in a row. 这是我的问题:当用户在一行中的任何位置进行更改时,我都有有效的代码可以插入用户名和时间戳。 Great! 大! So my code works and I answered my own question, right? 这样我的代码就可以工作了,我回答了自己的问题,对吗? Nope! 不! There's a tiny issue which, while it doesn't break the code, does lead to a user having their username input as having made a change when a change was not made . 有一个很小的问题,尽管它不会破坏代码,但确实导致用户输入用户名,而未进行更改则进行了更改。

Here's my code: 这是我的代码:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    ThisRow = Target.Row
    'protect Header row from any changes
    If (ThisRow = 1) Then
           Application.EnableEvents = False
           Application.Undo
           Application.EnableEvents = True
           MsgBox "Header Row is Protected."
           Exit Sub
    End If
    For i = 1 To 61
        If Target.Column = i Then
               ' time stamp corresponding to cell's last update
               Range("BK" & ThisRow).Value = Now
               ' Windows level UserName | Application level UserName
               Range("BJ" & ThisRow).Value = Environ("username")
           Range("BJ:BK").EntireColumn.AutoFit
        End If
    Next i
End Sub

Here's how it happens: A user decides they want to make a change to a cell, so they double click the cell. 它是这样发生的:用户决定要更改单元格,因此他们双击该单元格。 Now, if they push the escape key, nothing happens and everything is hunky dory. 现在,如果他们按下转义键,则什么也不会发生,并且一切都是笨拙的。 But, if they double click the cell, then click outside of the cell to another cell to leave that cell, the system logs that as a change even though no change was made and the user's username is put into column 62. This is no bueno, because someone could be held responsible for a mistake that another individual has made if they're incorrectly put down as the last person to change something in that row. 但是,如果他们双击该单元格,然后在该单元格之外单击到另一个单元格以离开该单元格,则即使没有进行任何更改并且用户的用户名已放入第62列,系统也会将其记录为更改。这不是bueno ,因为如果某个人被错误地放为最后一行更改该行中的某些内容,则可能会对另一个人犯的错误负责。

Conversely - it might be worthwhile to create a comment in a cell which is changed by a user, but I reckon I'd have the same issue with double-clicking a cell, so I'd still have to account for it. 相反,在由用户更改的单元格中创建注释可能是值得的,但是我认为双击单元格会遇到相同的问题,因此我仍然必须考虑它。

Thoughts? 思考?

Edit: Full disclosure, I found this code elsewhere and adapted it to my purposes. 编辑:完全公开,我在其他地方找到了此代码,并对其进行了修改以适应我的目的。

You can test to see if the old value and the new value are the same. 您可以测试一下旧值和新值是否相同。 I use "new" loosely, meaning excel things that the cell was edited so it's a "new" value in terms of the Worksheet_Change event understanding. 我宽松地使用“ new”,意思是擅长编辑单元格,因此就Worksheet_Change事件的理解而言,它是“ new”值。

I also got rid of your For loop as it seemed very unnecessary. 我也摆脱了您的For循环,因为这似乎非常不必要。 If I am mistaken, I apologize. 如果我弄错了,我深表歉意。

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    Dim ThisRow As Long ' make sure to declare all the variables and appropiate types
    ThisRow = Target.Row

    'protect Header row from any changes
    If (ThisRow = 1) Then

           Application.EnableEvents = False
           Application.Undo
           Application.EnableEvents = True
           MsgBox "Header Row is Protected."
           Exit Sub

    End If

    If Target.Column >= 1 And Target.Column <= 61 Then

        Dim sOld As String, sNew As String
        sNew = Target.Value 'capture new value

        With Application
            .EnableEvents = False
            .Undo
        End With

        sOld = Target.Value 'capture old value
        Target.Value = sNew 'reset new value

        If sOld <> sNew Then

            ' time stamp corresponding to cell's last update
            Range("BK" & ThisRow).Value = Now
            ' Windows level UserName | Application level UserName
            Range("BJ" & ThisRow).Value = Environ("username")
            Range("BJ:BK").EntireColumn.AutoFit

        End If

        Application.EnableEvents = True

    End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM