【问题标题】:Merge two Worksheet_Change events in VBA excel在 VBA excel 中合并两个 Worksheet_Change 事件
【发布时间】: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 列)。

标签: excel vba


【解决方案1】:

我重构了下面的代码。这应该有效。 BigBen 在他的评论中是正确的。

Private Sub Worksheet_Change(ByVal Target As Range)

Application.ScreenUpdating = False
    
Dim oRng As Range
Dim oCell As Range

If Not Intersect(Target, Target.Parent.Range("A:A")) Is Nothing 

    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" or oCell.Value = "Black" Then
                oCell.Value = "Grey"
            End If
        End If
    Next

End If

If Not Intersect(Range("B:B"), Target) Is Nothing Then
    
    If Target.Value = "Grey" Then
        
        Call Mail1
            
    End If
            
ElseIf Not Intersect(Range("C:C"), Target) Is Nothing Then
    
    If Target.Value = "Grey" Then
        
        Call Mail2
             
    End If
        
End If
        
Application.ScreenUpdating = True
    
End Sub

【讨论】:

  • Target.Parent 也可以简化为 Me
  • 效果很好。即时帮助。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
相关资源
最近更新 更多