【发布时间】:2021-01-09 05:39:56
【问题描述】:
我无法将两个事件合并为一个。
每当“A” 在同一行更改值。
代码 1:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Dim oRng As Range
Dim oCell As Range
If Intersect(Target, Target.Parent.Range("A:A")) Is Nothing Then Exit Sub
Set oRng = Target.Parent.Range("B" & Target.Row & ":" & Target.Parent.Cells(Target.Row, Target.Parent.UsedRange.Columns.Count).Address)
For Each oCell In oRng
If oCell.Value = "Black" Then
oCell.Value = "Grey"
End If
Next
For Each oCell In oRng
If oCell.Value = "White" Then
oCell.Value = "Grey"
End If
Next
Application.ScreenUpdating = True
End Sub
每当该列上的 "Black/WHite" 更改为 "Grey" 时,第二个代码都会调用某个邮件宏。
代码 2:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Intersect(Range("B:B"), Target) Is Nothing Then
If Target.Value = "Grey" Then
Call Mail1
End If
ElseIf Intersect(Range("C:C"), Target) Is Nothing Then
If Target.Value = "Grey" Then
Call Mail2
End If
End If
Application.ScreenUpdating = True
End Sub
这两个代码都可以单独工作,但是在尝试合并它们时我无法让宏调用工作,但我也没有收到某种错误消息。它只是不调用宏。
例子:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Dim oRng As Range
Dim oCell As Range
If Intersect(Target, Target.Parent.Range("A:A")) Is Nothing Then Exit Sub
Set oRng = Target.Parent.Range("B" & Target.Row & ":" & Target.Parent.Cells(Target.Row, Target.Parent.UsedRange.Columns.Count).Address)
For Each oCell In oRng
If oCell.Value = "White" Then
oCell.Value = "Grey"
End If
Next
For Each oCell In oRng
If oCell.Value = "Black" Then
oCell.Value = "Grey"
End If
Next
If Intersect(Range("B:B"), Target) Is Nothing Then
If Target.Value = "Grey" Then
Call Mail1
End If
ElseIf Intersect(Range("C:C"), Target) Is Nothing Then
If Target.Value = "Grey" Then
Call Mail2
End If
End If
Application.ScreenUpdating = True
End Sub
有什么建议吗?谢谢大家
【问题讨论】:
-
If Intersect(Target, Target.Parent.Range("A:A")) Is Nothing Then Exit Sub- 如果不满足第一个条件,则退出...这很可能会阻止您进入下一个条件(B 列)。