简体   繁体   English

Excel合并两列

[英]Excel merging two columns

I have two columns, each with slightly different list of countries 我有两列,每列的国家/地区列表略有不同

eg 例如

 A         B         C
 Japan     Japan
 US        China
 UK        Brazil
 India     Canada
 China     Mexico
 Brazil    Germany
           France
           Peru

How do I "merge" the two columns so that I can have values that exist in either column A or B in column C, without repeating the values? 如何“合并”两列,以便我可以拥有存在于C列的A列或B列中的值,而无需重复这些值?

ie

 A         B         C
 Japan     Japan     Japan
 US        China     US
 UK        Brazil    UK
 India     Canada    India
 China     Mexico    China
 Brazil    Germany   Brazil
           France    Canada
           Peru      Mexico
                     Germany
                     France
                     Peru

NOTE: I am currently using excel 2003 注意:我目前正在使用excel 2003

thanks a lot! 非常感谢!

EDIT: Here's my current attempts, in "exports and imports test" sheet, column G, I'm trying to VLOOKUP from the source of both columns A and D and "merge" it with an OR formula, and then sort it properly on the next column. 编辑:这是我目前的尝试,在“进出口测试”工作表的G列中,我试图从A列和D列的来源进行VLOOKUP并将其与OR公式“合并”,然后对它进行正确排序下一栏。 It's got an N/A error right now unfortunately... 不幸的是,现在有一个N / A错误...

http://wikisend.com/download/391826/Sample%20test.xls http://wikisend.com/download/391826/Sample%20test.xls

EDIT2: I tried using the macro posted below, while editing (by guessing) it to suit my needs, and the macro became this: EDIT2:我尝试使用下面发布的宏,同时根据自己的需要进行编辑(通过猜测),宏变成了:

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("G3").Select
    ActiveSheet.Paste
    Range("D3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("G3").Select
    Selection.End(xlDown).Select
    Range("G30").Select
    ActiveSheet.Paste
    Range("G3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    ActiveSheet.Range("$G$3:$G$60").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub

And here is the result (column G): 结果如下(G列):

在此处输入图片说明

It didn't quite work (in that duplicates exist) and I got an “Object doesn't support support this property or method” error. 它不是很有效(因为存在重复项),并且出现“对象不支持支持此属性或方法”错误。

This is kind of a hack, but you could do this: 这是一种hack,但是您可以这样做:

  1. Paste both columns into the same column (A). 将两列粘贴到同一列(A)中。
  2. Sort column A. 对A列进行排序。
  3. Create a new column (B) beside it and put the formula =IF(A2=A1, "", A2) , and drag down. 在其旁边创建一个新列(B),然后将公式=IF(A2=A1, "", A2)放下。 This will print out the word only if it is different from the word above it. 仅当单词与其上方的单词不同时,才会打印出该单词。
  4. Copy and paste by value in column B, and then sort column B to put the empty cells on top. 按值复制并粘贴到B列中,然后对B列进行排序以将空单元格放在顶部。
  5. Delete the empty cells. 删除空单元格。

WHat about a macro like this (I'm assuming data are in columns A, B, and C starting at row 2 and without gaps): 关于这样的宏(我假设数据位于第2行开始且没有间隙的A,B和C列中):

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("A2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("C2").Select
    ActiveSheet.Paste
    Range("B2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("C2").Select
    Selection.End(xlDown).Select
    Range("C10").Select
    ActiveSheet.Paste
    Range("C2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    ActiveSheet.Range("$C$2:$C$16").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub

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

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