合并 2 个工作表更改事件 Excel VBA

问题描述 投票:0回答:1

有人可以帮我将下面这两个事件结合起来吗?我的目标是当“已完成”输入到 F 列时将我的行移动到另一张表。此外,我希望在相应行中的值/字符串出现时将最后更新的日期/时间添加到 G 列改变了,但我不知道如何结合这些。请帮忙!

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim AffectedRange As Range
    Set AffectedRange = Intersect(Target, Target.Parent.Range("B3:L5000"))
    Dim Z As Long
    Dim xVal As String
    On Error Resume Next
    

    If Not AffectedRange Is Nothing Then
        Application.EnableEvents = False 'pervent triggering another change event

' Last Updated

    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Row < 1 Then Exit Sub
    If Target.Column < 2 Then Exit Sub
    Cells(Target.Row, "G") = Date
    
Next AffectedRange

'MOVING COMPLETED

    'Dim Z As Long
    'Dim xVal As String
    'On Error Resume Next
    
    If Intersect(Target, Range("F:F")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    For Z = 1 To Target.Count
        If Target(Z).Value > 0 Then
            Call MoveBasedOnValue
        End If
        
          
    Next
    
    Application.EnableEvents = True
    
    

    End If
End Sub
excel vba range
1个回答
0
投票

您可以像这样使用 Goto 或 GoSub :

If Target.Column = 6 and Target.Value  = "Completed' Then GoSub YourCodeForFColumn


'.. (more code here) 

exit sub
YourCodeForFColumn:

'  .. (code you want to run here)

Return
end sub

https://learn.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/gosubreturn-statement

© www.soinside.com 2019 - 2024. All rights reserved.