突出显示手动修改的单元格,而不是通过宏完成。

时间:2017-09-29 09:04:58

标签: vba excel-vba loops excel

我一直在做一些研究,如果它可以突出显示已经手动修改的细胞(通过输入信息)作为反对通过marco。我真正遇到的是跟踪变化,这可能不适用于这种情况

方案

我有一个宏,每次双击某些单元格时都会运行。当双击一个单元格时,会提供一个时间戳,这是完全正常的。

问题

我试图避免用户尝试手动修改时间戳或该相关单元格中的任何信息的情况。一旦更新,我试图使用锁定细胞的方法来避免。

要实现的解决方案。

如果用户已手动更新其突出显示的单元格,是否可能。但是,如果通过使用宏来更新它,那就好了。

代码

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Application.EnableEvents = False

    With Target
        If .Column = 4 Then
            Select Case .Row
                Case 20, 24, 25, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 40, 42, 43, 44, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65

                    .Value2 = "Prepared By" & "  " & Environ("Username")
                    .Value2 = .Value2 & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")

                    End Select
                  End If
    End With

1 个答案:

答案 0 :(得分:0)

最简单的解决方案就是使用change事件强制对所选单元格进行任何更改。我假设双击方面是为了使输入更加用户友好,所以我们不会删除它,而是复制它:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
With Target
    If .Column = 4 Then
        Select Case .Row
            Case 20, 24, 25, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 40, 42, 43, 44, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65
                .Value2 = "Prepared By" & "  " & Environ("Username") & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")
        End Select
    End If
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If Target.Cells.Count = 1 Then
    With Target
        If .Column = 4 Then
            Select Case .Row
                Case 20, 24, 25, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 40, 42, 43, 44, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65
                    .Value2 = "Prepared By" & "  " & Environ("Username") & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")
            End Select
        End If
    End With
Else
    For Each cell In Target.Cells
    With cell
        If .Column = 4 Then
            Select Case .Row
                Case 20, 24, 25, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 40, 42, 43, 44, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65
                    .Value2 = "Prepared By" & "  " & Environ("Username") & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")
            End Select
        End If
    End With
    Next cell
End If
End Sub