[英]Excel Macro (VBA) that checks cell values against a list in another workbook
I have two workbooks, one that is data (We'll call WB 1) that needs the values in column k checked against values on another workbook (We'll call WB 2) that contains the true values for the entries.我有两个工作簿,一个是数据(我们称为 WB 1),它需要对照包含条目真实值的另一工作簿(我们称为 WB 2)上的值检查列 k 中的值。 Each entry has a quantity attached to it so the price varies dependent on that.
每个条目都有一个附加的数量,因此价格因数量而异。 WB 1 is in a list with entries on each row and WB 2 has an item on each row and each column is the price for that quantity.
WB 1 在一个列表中,每行都有条目,WB 2 每行都有一个项目,每列是该数量的价格。 I need a macro that will check these values against each other and replace any values on WB 1 with the correct value from WB 2.
我需要一个宏来相互检查这些值,并用 WB 2 中的正确值替换 WB 1 上的任何值。
Example Data:示例数据:
WB 1
| Column A | Column C | Column K |
---------------------------------
| Item Name | 30 | $5.42 |
WB 2
| Column A | Column F | Column G | Column H | ... |
15 30 45
--------------------------------------------------
| Item Name | $2.86 | $4.53 | $6.00 | ... |
you can use vlookup to perform a search and follow the instruction below for better performance.您可以使用 vlookup 执行搜索并按照以下说明进行操作以获得更好的性能。
set the WB1 as source and WB2 as destination work.将 WB1 设置为源工作,将 WB2 设置为目标工作。
select the item in WB1 with a "For loop".使用“For 循环”选择 WB1 中的项目。 Make sure you select the appropriate column and its tuple
确保选择适当的列及其元组
For rowIndex = 2 To lastRow对于 rowIndex = 2 到 lastRow
(Alternatively use a range) (或者使用范围)
Perform a vlookup between the search.在搜索之间执行 vlookup。
paste/display the output in required column在所需列中粘贴/显示输出
Increment the loop (Next Row index).递增循环(下一行索引)。
(let me know if you need exact code snippets) (如果您需要确切的代码片段,请告诉我)
Regards, Mani问候, 玛尼
Here is the code snippet这是代码片段
*points to remember *要记住的要点
Phase 1: Loading the workbook阶段 1:加载工作簿
Phase 2: Calculation/Manipulation/Logic阶段 2:计算/操作/逻辑
lastRow = SourceData.Rows.Count
For rowIndex= 2 to lastRow
lTemp = sourceWorksheet.Cells(rowIndex, "Q").Value
lDigit = VLOOKUP( value, table, index_number, [not_exact_match] ) 'not_exact_match is usually false
DestinationWorksheet.Cells(rowIndex, "AM").Value = lDigit
next rowIndex
[Remember to declare the variables with appropriate datatypes matching your requirement]/ [记得使用符合您要求的适当数据类型声明变量]/
Regards, Mani问候, 玛尼
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.