简体   繁体   English

VBA:通过多个列进行vlookup

[英]VBA: vlookup through multiple columns

this is my first question so I will try my best to format this. 这是我的第一个问题,所以我将尽力格式化此问题。 I would like to use the vlookup function in more than just one column. 我想在多个栏目中使用vlookup函数。 It seems impossible to do is in the formula for the large data set, so maybe someone with VBA knowledge can help me. 似乎不可能在大数据集的公式中进行操作,因此也许具有VBA知识的人可以为我提供帮助。

In my excel file I've two spreadsheets: Sheet1 with one column "A" with 16868 different processes ID's. 在我的excel文件中,我有两个电子表格:具有1列“ A”和16868个不同进程ID的Sheet1。 Sheet2 with columns from "A" to "HX", where "A:HW" are different processes ID's (each column have different numbers of rows) and in "HX" column I have a region for the particular row of processes. 具有从“ A”到“ HX”的列的Sheet2,其中“ A:HW”是不同的进程ID(每列具有不同的行数),在“ HX”列中,我有一个特定进程行的区域。

What I am trying to achieve is to do vloop for sheet1 column "A" that will look for each record in table sheet2 and return "HX" when found it. 我想要实现的是对sheet1列“ A”进行vloop ,该列将在表sheet2中查找每个记录,并在找到它时返回“ HX”。

In example: I'm looking for "A2" cell in sheet1 in table_array sheet2 "A:HX". 在示例中:我在table_array sheet2“ A:HX”中的sheet1中寻找“ A2”单元格。 If found it then return cell in the same row but from column "HX". 如果找到,则返回同一行中的“ HX”列中的单元格。 The trick here is when the looking value is not in the sheet2 column "A", then it should vlookup in column "B", then "C" and so on till "HW". 这里的技巧是,当外观值不在sheet2列“ A”中时,则应在列“ B”中依次查找,然后在“ C”中依次类推,直到“ HW”。 There is an option where the looking value might not be in sheet2 at all and then the formula should return "0". 有一个选项,其中查找值可能根本不在sheet2中,然后公式应返回“ 0”。 Is it possible to do this in VBA or excel formula? 是否可以在VBA或excel公式中执行此操作?

Firstly, please NEVER use VLOOKUP . 首先,请不要使用VLOOKUP Ever. 曾经

An INDEX/MATCH combination is always better in every way. INDEX/MATCH组合在各个方面总是更好。 It is faster, more flexible and more robust as it does not break if you insert columns. 它更快,更灵活,更强大,因为如果您插入列,它不会中断。 It is also (arguably) easier to use as you do not need to count columns. 由于不需要计数列,因此(可以说)它也更易于使用。

Anyway, back to the problem at hand. 无论如何,回到眼前的问题。 You can use an INDEX/SUMPRODUCT combination for this: 您可以为此使用INDEX/SUMPRODUCT组合:

在此处输入图片说明

=INDEX(<range of return values>),SUMPRODUCT(--MAX((<range to search in>=<value to search for>)*ROW(<range to search in>)))-(ROW(<range to search in>)-1))

The SUMPRODUCT returns the last row number where the value is found. SUMPRODUCT返回找到该值的最后一个行号。

The last bit -(ROW(<range to search in>)-1) is just to make the returned value relative to the searched range (rather than an absolute row number. 最后一位-(ROW(<range to search in>)-1)只是使返回值相对于搜索范围(而不是绝对行号)。

The INDEX is then uses this value to select a value from the HX column. 然后,INDEX使用该值从HX列中选择一个值。

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

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