一旦单元格数据发生更改,我的VBA就不会运行
在我的工作表中,我在F列和G列中运行VBA。
G列有数据验证,我希望根据F列中输入的数值触发该数据验证。
示例:
列F的数值为2.5,这导致列G显示"0";良好的站立">
如果我将F列的值更改为<2,如果F列的值最初<2,并且我将其增加到>2列G将显示";良好的声誉。
Private Sub Worksheet_Change(ByVal Target As Range)
StrtRow = 2
EndRow = Range("F" & Rows.Count).End(xlUp).Row
For i = StrtRow To EndRow
If Range("F" & i).Value >= 2 Then
Range("G" & i).Value = "Good Standing"
End If
Next
End Sub
如果< 2
,则缺少用于清除内容的Else
子句。考虑到@SJR的评论,尝试这个(未测试(
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngWatched As Range: Set rngWatched = Me.Range("F:F")
Dim cl As Range
If Not Intersect(rngWatched, Target) Is Nothing Then
Application.EnableEvents = False
For Each cl In Intersect(rngWatched, Target)
If cl.Value >= 2 Then
cl.Offset(0, 1).Value = "Good Standing"
Else
cl.Offset(0, 1).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub