简体   繁体   English

Excel 宏根据匹配两个值(行/列)查找单元格并替换单元格中的值

[英]Excel Macro to find a cell based on matching two values (Row/Column) and replace value in cell

I am having some trouble trying to find a solution to an issue I have.我在尝试解决我遇到的问题时遇到了一些麻烦。 I have two excel sheets that I would like to find or index values from one sheet to the other and then replace the value of the cell after locating the cell.我有两张 excel 表格,我想从一张表格查找或索引另一张表格的值,然后在找到单元格后替换单元格的值。

I think maybe a better way to picture is that I have a excel sheet "Data1" that consists of three columns with rows of data.我认为也许更好的描绘方式是我有一个 excel 表“Data1”,它由三列和多行数据组成。

Example of Sheet 1:表 1 示例:

      A          B                c
1 | Header 1 | Header 2       | Header 3 |
2 | -------- | -------------- |----------|
3 | 123456   | AABB           |12AB12AB  |
4 | 678910   | BBCC           |34CD34CD  |

I have a second Excel Sheet "Data2" that consists of multiple columns and rows of data.我有第二个 Excel 表“Data2”,它由多列和多行数据组成。

Example Sheet "Data2":示例表“Data2”:

      A         B            C          D         E
1 | Header1  | BBCC       | CCDD     | AABB    | EEFF    |
2 | -------- | -----------|----------|---------|---------|
3 | 123456   | ValueX     | ValueY   |  ValueZ | ValueB  |
4 | 678910   | ValueXX    | ValueXY  | ValueYY | ValueZZ |

What I would like to do is run a macro:我想做的是运行一个宏:

To first, match the value A3 (123456) in Sheet "Data1" to the same value in Column A from Sheet "Data2"首先,将工作表“Data1”中的值 A3 (123456) 与工作表“Data2”中的 A 列中的相同值匹配

Second, match the value B3 (AABB) in Sheet "Data1" to the same value in Row 1 from Sheet "Data2"其次,将工作表“Data1”中的值 B3 (AABB) 与工作表“Data2”中第 1 行中的相同值匹配

Third, replace the value in the corresponding cell with the value from Sheet "Data1" cell C3.第三,将相应单元格中的值替换为工作表“Data1”单元格 C3 中的值。

Then, Loop and replace all cells until the rows of data from Sheet "Data1" end.然后,循环并替换所有单元格,直到工作表“Data1”中的数据行结束。

The Values in Sheet "Data2" should then change to look like:然后工作表“Data2”中的值应更改为:

       A         B            C          D          E
1 | Header1  | BBCC       | CCDD     | AABB     | EEFF    |
2 | -------- | -----------|----------|----------|---------|
3 | 123456   | ValueX     | ValueY   | 12AB12AB | ValueB  |
4 | 678910   | 34CD34CD   | ValueXY  | ValueYY  | ValueZZ |

Please let me know what you think and I hope I laid out the requirements as simple as possible.请让我知道您的想法,我希望我尽可能简单地列出要求。 Thank you for your time and support to help me work this solution.感谢您的时间和支持,以帮助我解决这个问题。 Cheers干杯

IF your version of Excel supports XLOOKUP then you can set up Sheet "Data3" with the same column and row headers and use this functions for the data cells.如果您的 Excel 版本支持XLOOKUP ,那么您可以设置具有相同列和行标题的工作表“Data3”并将此功能用于数据单元格。

=XLOOKUP($A3&B$1,Data1!$A$3:$A$4&Data1!$B$3:$B$4,Data1!$C$3:$C$4,Data2!B3)

It takes the data in data2 and overwrites it with the data from data1 if there is a match.如果匹配,它会获取 data2 中的数据并用 data1 中的数据覆盖它。

样品溶液

More on XLOOKUP 更多关于 XLOOKUP

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

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