[英]Excel VBA - Cell change, datestamp in a column
我想創建兩個日期戳,一個用於OK,一個用於NOT OK。 值在A列中填寫, 'x' for OK
為'x' for OK
'NOK' for NOT OK
。
在A列中填寫“ x”時,第49列中的值應為“ x”填寫時的日期戳。
當在列中填寫“ NOK”時,列52中的值應為在填寫“ NOK”時的日期戳。
同樣,如果從A列中刪除了“ x”或“ NOK”,則日期戳也應消失。 這就是我所擁有的。
Private Sub Worksheet_Change(ByVal Target As Range)
'49 = ok
'52 = NOK
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set LastRow = Range("A" & Rows.Count).End(xlUp).Row
Set KeyCells = Range("A1:" & LastRow)
If Application.Intersect(KeyCells, Range(Target.Address)) Like "*x*" Then
Cells(Row, 49).Value = Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
End If
If Application.Intersect(KeyCells, Range(Target.Address)) Like "*NOK*" Then
Cells(Row, 52).Value = Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
End If
End Sub
在這里,嘗試一下:
Private Sub Worksheet_Change(ByVal Target As Range)
'49 = ok '52 = NOK
Application.EnableEvents = False
With Target
'check if change happend in column A
If .Column = 1 Then
'check if changed value is X
If .Value Like "*x*" Then
'add datestamp if it is
Cells(.Row, 49).Value = Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
Else
'clear datestamp if not
Cells(.Row, 49).Value = ""
End If
If .Value Like "*NOK*" Then
Cells(.Row, 52).Value = Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
Else
Cells(.Row, 52).Value = ""
End If
End If
End With
Application.EnableEvents = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.