使用命令按钮在Excel中复制数据而不重复

时间:2018-09-12 10:03:21

标签: excel vba

如果满足条件,我创建了一个命令按钮,用于将数据从一个工作表传输到另一个工作表。我想问一下如果多次使用该按钮,如何防止该按钮重复。例如:如果我将其他数据添加到需要复制的第一张纸上。下面是我用于按钮的代码。

run()

1 个答案:

答案 0 :(得分:0)

您可以使用删除重复步骤。我做了一些编辑,您可以在下面看到:

Sub MyCammand()

Dim AllSheet As Worksheet
Dim LendSheet As Worksheet
Dim LastRow As Integer
Dim RowCnt As Integer
Dim DestRow As Integer

Set AllSheet = ActiveWorkbook.Sheets("All")
Set LendSheet = ActiveWorkbook.Sheets("Lending")

With AllSheet
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    DestRow = LendSheet.Range("A" & LendSheet.Rows.Count).End(xlUp).Row + 1
    For RowCnt = 2 To LastRow
        If .Cells(RowCnt, 3).Value = "X" Or .Cells(RowCnt, 3).Value = "x" Then
            LendSheet.Rows(DestRow).Value = .Rows(RowCnt).Value
            DestRow = DestRow + 1
        End If
    Next
End With
'..... Remove Duplicates
Dim LastCol As String
With LendSheet
    LastCol = Split(.Range("A1").End(xlToRight).Address, "$")(1)
    .Range("A:" & LastCol).RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7), Header:=xlYes
End With

结束子