我失去了理智:
我在我的工作表2中有这个代码,它的工作效果很好(参见代码图片):
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A5:D150")) Is Nothing Then Cancel = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A6:D150")) Is Nothing Then Exit Sub
On Error GoTo ExitPoint
Application.EnableEvents = False
If Not IsDate(Target(1)) Then
Application.Undo
MsgBox " You can't delete cell content :)" _
, vbCritical, "Sara"
End If
ExitPoint:
Application.EnableEvents = True
End Sub
现在我有一张Excel表格,即单元格A:AC。
我希望将公式应用于A:D
,跳过E
,应用于F:V
,跳过W:Z
,应用于AA:AC
。
如何将这些代码实现到多个范围?
我尝试了很多选项但都失败了。我非常感谢任何帮助和反馈。
答案 0 :(得分:2)
您可以尝试使用Union
。类似的东西:
Dim r As Range
Set r = Union(Range("A6:D150"),Range("F6:V150"),Range("AA6:AC150"))
If Intersect(Target, r) Is Nothing Then
'rest of your code
或者您可以直接使用非连续范围表示法:
If Intersect(Target, Range("A6:D150,F6:V150,AA6:AC150")) Is Nothing Then
'rest of your code