如何比较Vba中选定范围和列之间的值

时间:2017-11-06 05:32:47

标签: excel vba excel-vba

我写了这个脚本。它将用户选择的列复制到另一个选项卡,并在最后一行之后打印它们。我想开发它。当它想要将单元格复制到另一个选项卡时,找到与目标列匹配的值并重写它。因此它避免了重复的值。我是Vba的新手,请帮助我:)非常感谢。

Private Sub CommandButton1_Click()

    Dim Result As Excel.Worksheet
    Set Result = ActiveWorkbook.Worksheets("Result")

    Dim rColumn As Range, rRow As Range

    X = Sheets("Result").Cells(Rows.Count, 1).End(xlUp).Row 'Used range in A approx.

    For Each rRow In Selection.Rows

        For Each rColumn In Selection.Columns

            Result.Cells(X + 1, rColumn.Column).Value = Cells(rRow.Row, rColumn.Column).Value

        Next

        X = X + 1
    Next

End Sub

Function hasColumn(Target As Range, vColumn As Variant)
    hasColumn = Not Intersect(Target, Target.Parent.Columns(vColumn)) Is Nothing
End Function

0 个答案:

没有答案