简体   繁体   English

Excel VBA:查找两个表的并集

[英]Excel VBA: find the union of two tables

I am new to and I've got two updating columns of text & I want to find the union of the two columns. 我是新手,并且我有两个文本更新列,而且我想找到两列的并集。

For Example 例如

+---------+---------+
| ColumnA | ColumnB |
+---------+---------+
|   ABC   |    F    |
|   DE    |    F    |
|   F     |   AB    |
|   DBCA  |   CDE   |
|   E     |         |
+---------+---------+

I want to write the result in a new column columnC ( order doesn't matter ) 我想将结果写入新列columnC顺序无关紧要

+---------+
| ColumnC |
+---------+
|   ABC   |
|   F     |
|   DE    |
|   DBCA  |
|   E     |
|   AB    |
|   CDE   |
+---------+

How to work out this? 如何解决这个问题? Thanks. 谢谢。

edited : added removal of duplicated values 编辑 :添加删除重复值

try this 尝试这个

Sub main()
    With Worksheets("Columns") '<--| change "Columns" to your actual worksheet name
        With .Range("A1", .Range("A1").End(xlDown))
            .Parent.Range("C1").Resize(.Rows.Count).Value = .Value
        End With
        With .Range("B1", .Range("B1").End(xlDown))
            .Parent.Range("C1").End(xlDown).Offset(1).Resize(.Rows.Count).Value = .Value
        End With
        .Range("C1", .Range("C1").End(xlDown)).RemoveDuplicates Array(1)
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM