VBA:检查下拉列表以获取更新和复制行(如果需要)

时间:2017-06-09 13:22:33

标签: excel vba excel-vba worksheet-function

我想创建一个宏,它检查列“I:I”以查找下拉列表中的任何更改。此下拉列表包含多个条目。输入“Declined”条目后,它应将整行复制到另一个工作表(“Declined”)并删除主工作表中的行(“Data”)。这也意味着,该行将始终复制到工作表中的上一个条目下方(“已拒绝”)。

我从未使用过Worksheet_Change,也没有使用过相交功能,所以感谢每一个帮助。

提前感谢您的支持。

1 个答案:

答案 0 :(得分:0)

我刚刚找到了解决方案,感谢您的想法:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim shtDeal As String
Dim shtDecl As String
Dim lastrow As Integer

shtDeal = "Name 1"
shtDecl = "Name 2"


If Not Intersect(Target, Range("F1:F10000")) Is Nothing Then

    On Error GoTo ErrHelp

    If Target = "Declined by x" Or _
    Target = "Declined by y" Or _
    Target = "Declined by z" Then

        lastrow = Worksheets(shtDecl).Range("A" & Rows.Count).End(xlUp).Row + 1
        Target.EntireRow.Copy Destination:=Worksheets(shtDecl).Range("A" & lastrow)
        MsgBox "Auf Grund Ihrer Auswahl wurde diese Datenreihe auf das Arbeitsblatt 'Declined' verschoben."
        Target.EntireRow.Delete

    End If

End If

Done:
Exit Sub

ErrHelp:
End Sub

也许这可以帮助其他人解决同样的问题。