我能够运行验证和更改事件触发器以在一个单元格上工作(这里引用的是M6)。当用户从下拉列表中选择“有效”或“无效”时,它应该在相邻列(N6,O6)中填充用户名和日期,如果我从下拉列表中选择选项或复制一次估计一个单元格。
当我一次复制多个单元格(M8:M10)中的值时,宏不起作用,当使用自动填充选项填充该列单元格中的记录时,它也不起作用。还尝试插入无效数据“adsadasdad “在Cell M8中,验证工作正常,但在多个单元格中插入无效数据时,验证无效。
请找到宏代码
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRow As Long
Set MainWB = ThisWorkbook
LastRow = MainWB.Worksheets("LDVC_data").Cells(MainWB.Worksheets("LDVC_data").Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
If Target.Address = Range("M" & i).Address Then
If Range("M" & i) = "Valid" Or Range("M" & i) = "Not Valid" Then
ActiveSheet.Range("N" & i).Value = (Environ$("Username"))
ActiveSheet.Range("O" & i).Value = Now
ElseIf (Range("M" & i) = "[enter image description here][1]") Then
ActiveSheet.Range("N" & i).Clear
ActiveSheet.Range("O" & i).Clear
Else[enter image description here][1]
MsgBox ("Kindly enter valid value")
Range("M" & i) = ""
End If
End If
Next i
End Sub
答案 0 :(得分:0)
管理以捕获更改并在多个单元格中执行数据验证。 是否对代码进行了以下更改。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRow As Long
Set MainWB = ThisWorkbook
Dim myRange As Range
Set myRange = Target
LastRow = MainWB.Worksheets("LDVC_data").Cells(MainWB.Worksheets("LDVC_data").Rows.Count, "A").End(xlUp).Row
For Each targetCell In Target
For i = 2 To LastRow
If targetCell.Address = Range("M" & i).Address Then
If Range("M" & i) = "Valid" Or Range("M" & i) = "Not Valid" Then
ActiveSheet.Range("N" & i).Value = (Environ$("Username"))
ActiveSheet.Range("O" & i).Value = Now
ElseIf (Range("M" & i) = "") Then
ActiveSheet.Range("N" & i).Clear
ActiveSheet.Range("O" & i).Clear
Else
MsgBox ("Kindly enter valid value")
Range("M" & i) = ""
End If
End If
Next i
Next targetCell
End Sub