从下拉列表中选择值时输入时间戳

从下拉列表中选择值时输入时间戳

column A有数据验证,可以让用户从列表中选择一个值。

当选择值时Complete,我希望右侧的单元格column B显示更改的日期Complete

数据验证从运行开始,A1:A2500因此希望规则适用于B1:B2500

我尝试这样做只适用于 Cell A1& B1

我该如何修改它以适应所需的范围?

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("A1")) Is Nothing Then
       Select Case Range("A1").Value
           Case "Complete"
               Range("B1").Value = Now
           Case Else
               Range("B1").Value = 0
        End Select
    End If

End Sub

答案1

解决了

对于遇到类似问题的其他人来说,下面的方法对我来说很好用:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("A1:A2500"), Target)
xOffsetColumn = 1
If WorkRng = "Complete" Then
    Application.EnableEvents = False
    For Each Rng In WorkRng
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = Now
        Else
            Rng.Offset(0, xOffsetColumn).ClearContents
        End If
    Next
    Application.EnableEvents = True
End If
End Sub

相关内容