我有下面的代码工作正常,只允许一个条目存在于一行的一个范围内(即,B3:I3)。您如何扩展此代码以单独评估多行,而不必为每个新行复制,粘贴和调整此代码?换句话说,您如何确保B3:I3只允许1个条目; B4:I4只允许1个条目; B5:I5只允许1个条目;等等?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rg As Range
Dim c As Range
Set rg = Range("B3:I3")
'is the changed cell in the Range to check
If Not Intersect(Target, rg) Is Nothing Then
'Did you enter something in that cell, or just clear it?
If Len(Target) > 0 Then
'Don't trigger the event endlessly
Application.EnableEvents = False
For Each c In rg
'make sure to not clear the cell we just changed
If Intersect(c, Target) Is Nothing Then c.ClearContents
Next c
End If
End If
're-enable the event method
Application.EnableEvents = True
End Sub
字符串
这段代码在单行上运行良好,但我希望它在多行上重复。
2条答案
按热度按时间lsmepo6l1#
请试试看。
字符串
zlwx9yxi2#
你可能会在这之后(不太确定你的目标......)
字符串