简体   繁体   English

根据另一个工作簿中的列表检查单元格值的 Excel 宏 (VBA)

[英]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 执行搜索并按照以下说明进行操作以获得更好的性能。

  1. set the WB1 as source and WB2 as destination work.将 WB1 设置为源工作,将 WB2 设置为目标工作。

  2. 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) (或者使用范围)

  1. Perform a vlookup between the search.在搜索之间执行 vlookup。

  2. paste/display the output in required column在所需列中粘贴/显示输出

  3. 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:加载工作簿

  1. You would need to first refer the source workbook by path or call it as loading the workbook.您需要首先通过路径引用源工作簿或将其称为加载工作簿。
  2. Then assign the source workbook say(set sourceworkbook = "workbook derived from the path and assigned to a variable"然后分配源工作簿 say(set sourceworkbook = "从路径派生的工作簿并分配给一个变量"
  3. Many code is available in stackoverflow to achieve the same. stackoverflow 中提供了许多代码来实现相同的目的。

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.

相关问题 Excel 宏 - 针对另一个工作簿运行 - Excel Macro - Run Against Another Workbook Excel 2016 VBA宏-使用存储在另一个工作簿中的值更新excel工作簿中的各种命名范围 - Excel 2016 VBA Macro - update various named ranges in an excel workbook using values stored in another workbook 从列中选择每个单元格,并在另一个工作簿中的列中循环(如果存在)Excel VBA宏 - select each cell from a column and loop through a column in another workbook if it exists Excel VBA Macro VBA宏使用另一个工作簿中单元格的路径保存Excel文件 - VBA macro to save excel file using path from cell in another workbook 隐藏excel工作簿中的行,不包括单元格颜色 - vba宏 - Hide rows in excel workbook excluding cell color - vba macro 使用vba将单元格从一个工作簿复制到excel中的另一个工作簿 - Copying the cell from One workbook to another workbook in excel using vba VBA EXCEL 在不打开工作簿的情况下更新另一个工作簿中的单元格 - VBA EXCEL update cell in another workbook without opening the workbook Excel VBA:Shell可以在另一个Excel实例中的工作簿中运行宏 - Excel VBA: Can Shell run a macro in a workbook in another Excel instance Excel宏,用于将单元格内容复制到另一个工作簿中的特定工作表 - Excel macro for copying cell contents to a specific sheet in another workbook 是否可以使用VBA将宏从一个Excel工作簿复制到另一个工作簿? - Is it possible to copy a macro from one Excel workbook to another using VBA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM