【问题标题】:Combining Worksheet_Change events组合 Worksheet_Change 事件
【发布时间】:2022-08-17 16:47:55
【问题描述】:

我想将此代码与另一个执行相同操作的代码结合起来,但不是“关闭”,而是用于“等待 RA”和 sheet3。

Sub closed(Worksheet_Change)
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub
If Target.Value = vbNullString Then Exit Sub
If Intersect(Target, Columns(\"E:E\")) Is Nothing Then Exit Sub

Application.ScreenUpdating = False

If Target.Value = \"Closed\" Then
    Target.EntireRow.Copy Sheet2.Range(\"A:A\").End(3)(2)
    Target.EntireRow.Delete
End If
  
Sheet2.Columns.AutoFit

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
End Sub
  • 那么将Closed 更改为Waiting RA,将Sheet2 更改为Sheet3
  • 是啊,你要什么?您自己的问题提供了答案,那么您需要什么帮助确切地?
  • 我需要将它们结合起来

标签: excel vba


【解决方案1】:

像这样的东西应该工作:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    
    If Target.Count > 1 Then Exit Sub
    If Target.Value = vbNullString Then Exit Sub
    If Intersect(Target, Columns("E:E")) Is Nothing Then Exit Sub
    
    Select Case Target.Value             'check value and assign sheet if matched
        Case "Closed": Set ws = Sheet2
        Case "Waiting RA": Set ws = Sheet3
    End Select
    
    If Not ws Is Nothing Then            'any match?
        With Target.EntireRow
            'Not disabling events since this will not result in a loop
            '  (will be filtered out by the `Target.Count > 1` condition)
            .Copy Destination:=ws.Cells(Rows.Count, "A").End(xlUp).Offset(1)
            .Delete
        End With
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2021-06-01
    • 2019-06-08
    相关资源
    最近更新 更多