Excel 删除多个选定单元格中的数据时创建/删除时间戳
我在VBA中有一些代码,用于在用户编辑位于指定范围内的单元格内容时在Excel中创建/删除时间戳。它在后台工作,不需要用户有意识地激活(无按键命令) 然而,这似乎只有在用户只编辑一个单元格时才起作用。当选择多个单元格并且用户删除内容(而不是单元格本身)时,将弹出一个错误并提示用户调试代码 我的代码与其他一些在线资源纠缠不清,因此我并不声称完全理解正在发生的事情,但有一行代码导致了错误,它在我的代码中出现了两次,内容如下:Excel 删除多个选定单元格中的数据时创建/删除时间戳,excel,vba,timestamp,cell,erase,Excel,Vba,Timestamp,Cell,Erase,我在VBA中有一些代码,用于在用户编辑位于指定范围内的单元格内容时在Excel中创建/删除时间戳。它在后台工作,不需要用户有意识地激活(无按键命令) 然而,这似乎只有在用户只编辑一个单元格时才起作用。当选择多个单元格并且用户删除内容(而不是单元格本身)时,将弹出一个错误并提示用户调试代码 我的代码与其他一些在线资源纠缠不清,因此我并不声称完全理解正在发生的事情,但有一行代码导致了错误,它在我的代码中出现了两次,内容如下: If Target.Value = "" Then 我认为这基本上是说,
If Target.Value = "" Then
我认为这基本上是说,当用户编辑的单元格为空时,应该记录/删除时间戳。我认为它一次只看一个细胞,可能是因为
For Each Cell In Target
及
部分代码。
当有人从一个或多个单元格中删除信息/文本时,如何被动激活时间戳代码,而不会导致此错误
我的完整代码:
Public Sub DocChange(ByVal Target As Range)
' Auto Date
Dim Cell As Range 'Create the "Cell" dimension as a range
For Each Cell In Target 'Within our range, wherever we might click into a cell, we want to do the below stuff
If Target.Row <> Range("A:A").Row Then 'If the selection is not in the first row
If Cell.Value <> "" Then 'And it is not blank
Range("last_update").Value = Now 'Place the current time and date into the cell labeled "last_update"
Range("Last_User").Value = Environ("username") 'Place the username of the current user making the edit into the cell labeled "Last_User"
End If
' If Target.Column = 11 Then 'If the selection is in column 11 (K)
' If Target.Value = "" Then 'And if the cell is blank
If Target.Column = 11 Then 'If the selection is in column 11 (K)
ActivationStatus Target
' If Cell.Value = "" Then 'And if the cell is blank
' Cells(Target.Row, Target.Column - 1).Value = "" 'Then delete the date in the next column
' End If
End If 'And end the if loop
End If
If Cell.Row <> Range("A:A").Row Then 'If the cursor is placed not in Row 1
' If Not Application.Intersect(Target, Range("A1")) Is Nothing Then
If Cell.Value = "" Then 'And that cell is emptied
' If Target.Value = "" Then 'And that cell is emptied
Range("last_update").Value = Now 'Place the current time and date into the cell labeled "last_update"
Range("Last_User").Value = Environ("username") 'Place the username of the current user making the edit into the cell labeled "Last_User"
End If
End If
Next Cell 'Cycle to the next cell, basically telling the program to start looking for the next input
Application.EnableEvents = True 'Make sure that the user can perform operations within the doc
Application.ScreenUpdating = True 'Make sure that whatever has and will be done can be seen in the doc
End Sub
旁注:您是否碰巧将
工作表\u change
更改为DocChange
?在循环中使用Cell
而不是Target
。如果Target.Column=11,则。。。但如果目标是一个可能跨越多个列的范围,那么等于11意味着什么?这里需要的单元格不是targetcell
,而是target
似乎起作用的单元格。我得到了一个不同的错误,但我要看看我能做些什么来修复它。就VBA与文档的交互方式而言,Cell
和Target
之间有什么区别?我很好奇为什么一个行得通而另一个不行。Target.Column=11
只用于查找对特定列的更改。基本上,当用户在K列中将项目设置为“活动”时,我希望代码在J列的相邻单元格中记录时间戳。当“活动”被删除或从下拉列表中更改为另一个值时,它还需要删除时间戳。
Public Sub DocChange(ByVal Target As Range)
' Auto Date
Dim Cell As Range 'Create the "Cell" dimension as a range
For Each Cell In Target 'Within our range, wherever we might click into a cell, we want to do the below stuff
If Target.Row <> Range("A:A").Row Then 'If the selection is not in the first row
If Cell.Value <> "" Then 'And it is not blank
Range("last_update").Value = Now 'Place the current time and date into the cell labeled "last_update"
Range("Last_User").Value = Environ("username") 'Place the username of the current user making the edit into the cell labeled "Last_User"
End If
' If Target.Column = 11 Then 'If the selection is in column 11 (K)
' If Target.Value = "" Then 'And if the cell is blank
If Target.Column = 11 Then 'If the selection is in column 11 (K)
ActivationStatus Target
' If Cell.Value = "" Then 'And if the cell is blank
' Cells(Target.Row, Target.Column - 1).Value = "" 'Then delete the date in the next column
' End If
End If 'And end the if loop
End If
If Cell.Row <> Range("A:A").Row Then 'If the cursor is placed not in Row 1
' If Not Application.Intersect(Target, Range("A1")) Is Nothing Then
If Cell.Value = "" Then 'And that cell is emptied
' If Target.Value = "" Then 'And that cell is emptied
Range("last_update").Value = Now 'Place the current time and date into the cell labeled "last_update"
Range("Last_User").Value = Environ("username") 'Place the username of the current user making the edit into the cell labeled "Last_User"
End If
End If
Next Cell 'Cycle to the next cell, basically telling the program to start looking for the next input
Application.EnableEvents = True 'Make sure that the user can perform operations within the doc
Application.ScreenUpdating = True 'Make sure that whatever has and will be done can be seen in the doc
End Sub
Public Sub ActivationStatus(ByVal Target As Range)
'If Target.Column = 11 Then 'And it is in the 11th column (K)
Application.EnableEvents = False
If Cell.Value = "Active" Then 'And it has "Active" in it
Cells(Target.Row, Target.Column - 1).Value = Date 'Then put the today's date next to it
ElseIf Cell.Value = "Not Released" Then 'Or if it has "Not Released" in it
Cells(Target.Row, Target.Column - 1).ClearContents 'Then clear the date next to it
ElseIf Cell.Value = "No Status" Then 'Or if it has "No Status" in it
Cells(Target.Row, Target.Column - 1).ClearContents 'Then clear the date next to it
ElseIf Cell.Value = "Obsolete" Then 'Or if it has "Obsolete" in it
Cells(Target.Row, Target.Column - 1).ClearContents 'Then clear the date next to it
ElseIf Cell.Value = "" Then
Cells(Target.Row, Target.Column - 1).ClearContents
ElseIf Cell.Value = "Tested" Then
If IsDate(Cells(Target.Row, Target.Column - 1).Value) Then
Target.EntireRow.Hidden = True
Else
MsgBox "The selected project has no date of activation." _
& Chr(10) & "Please ensure that the project has a" _
& " recorded date of activation before declaring it tested." _
& Chr(10) & " Not Released --> Active --> Tested", vbExclamation
End If
End If
Application.EnableEvents = True
'End If
End Sub