vba Excel - 将一列与另一列进行比较并显示结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13808231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:45:50  来源:igfitidea点击:

Excel - Compare one column to another and display results

excelfunctionexcel-vbaexcel-formulavba

提问by Dan B. Lee

In Excel 2007, I have two columns, similar data (list of names). If the name is found in both columns I'd like to change a blank column to active. How can this be done?

在 Excel 2007 中,我有两列相似的数据(名称列表)。如果在两列中都找到了名称,我想将空白列更改为活动列。如何才能做到这一点?

Longer Description of what I'd like to do:

我想做的事情的详细描述:

Column A:A on Sheet 1 has 300 names. Column A:A on Sheet 2 has 20 names. If the names on Sheet 2 are also in Sheet 1 I'd like to fill Column A:B on Sheet 1 with the word "Active"

工作表 1 上的 A:A 列有 300 个姓名。工作表 2 上的 A:A 列有 20 个名称。如果工作表 2 上的名称也在工作表 1 中,我想用“活动”一词填充工作表 1 上的 A:B 列

How can this be done? Thanks in advance!

如何才能做到这一点?提前致谢!

回答by John Bustos

Assuming you had the names in Column A of both sheets, In cell B1 of Sheet1, put in the following formula:

假设您在两张工作表的 A 列中都有名称,在 Sheet1 的单元格 B1 中,输入以下公式:

=IF(ISNUMBER(MATCH(A1,Sheet2!A:A,0)),"Active","Not Active")

You can then drag that formula down - If the name exists in BOTH, it will say "Active" otherwise it will say "Not Active".

然后您可以将该公式向下拖动 - 如果名称存在于 BOTH 中,它将显示“活动”,否则它会显示“不活动”。

Hope this helps.

希望这可以帮助。

回答by Stewbob

This can be handled with a simple VLOOKUPformula:

这可以用一个简单的VLOOKUP公式来处理:

=IF(VLOOKUP(Sheet2!A1,Sheet1!A:A,1,FALSE)=A1,"Active","")

The above formula is placed in cell B1 on Sheet1 and then copied down as far as there is data in column A on sheet 1.

上面的公式放在 Sheet1 的 B1 单元格中,然后向下复制,直到工作表 1 的 A 列中有数据。

The only caveat with this formula is that the data in Sheet2 column A must be sorted alphabetically.

此公式唯一需要注意的是,Sheet2 列 A 中的数据必须按字母顺序排序。

The FALSEnear the end of the formula indicates that an exact match is required.

FALSE附近的公式的末尾指示精确匹配是必需的。