简体   繁体   English

Vlookup Vlookup的结果

[英]Vlookup the results of a Vlookup

In the past year I have become quite skilled in Excel, and I am ready for the next step and start using VBA. 在过去的一年中,我对Excel有了相当的熟练,可以为下一步做准备,并开始使用VBA。

I try to achieve the following: I want to create a tool that can run multiple Vlookups from different data sources, and combine all the data into one comprehensive overview. 我尝试实现以下目标:我想创建一种工具,该工具可以从不同的数据源运行多个Vlookups ,并将所有数据组合到一个全面的概述中。

  • Step one: Run a vlookup in worksheet1 using the data from worksheet2 步骤一:运行vlookupworksheet1使用来自数据worksheet2
  • Step two: The results that are generated in worksheet1 by the vlookup , is the new reference data to be used in a second vlookup using the data of a different worksheet worksheet3 步骤二: vlookup在worksheet1中生成的结果是新的参考数据,该数据将在第二个vlookup使用不同的工作worksheet3的数据

Now I tried to use for following code for that: 现在,我尝试使用以下代码:

Dim i As Long, r As Long
r = 4
For i = 1 To 10000

On Error Resume Next

Cells(r, 9).Value = WorksheetFunction.VLookup(Cells(r, 7).Value, Sheets("SPdata").Range("A3:V30321"), 9, 0)
Cells(r, 11).Value = WorksheetFunction.VLookup(Cells(r, 7).Value, Sheets("SPdata").Range("A3:V30321"), 11, 0)
Cells(r, 10).Value = WorksheetFunction.VLookup(Cells(r, 9).Value, Sheets("CREBS").Range("C2:D300"), 2, 0)

  r = r + 1

As you can see I try to use the results of Cells(r, 9) in the third vlookup, to match data from sheet CREBS. 如您所见,我尝试在第三个vlookup中使用Cells(r, 9)的结果来匹配工作表CREBS的数据。 The problem that I encounter is that the 3rd vlookup does not generate any results. 我遇到的问题是第3个vlookup不会生成任何结果。

Also, the On Error Resume Next works great but I would like to fill the blanks with "Not available" 另外,“ On Error Resume Next效果很好,但我想用“ Not available”填充空白

As I said, I'm quite new to this. 如我所说,我对此很陌生。 And my method is probably not the most efficient, but if anyone can help me solve the issues / or can suggest a more efficient code, I would be most grateful. 我的方法可能不是最有效的,但是如果有人可以帮助我解决问题/或者可以提出更有效的代码,我将不胜感激。

A few things: Don't use On Error Resume Next since it is not only a code-smell (and a pungent one at that) but it will also skip past any errors that would otherwise raise your attention to a bug. 几件事:不要使用On Error Resume Next因为它不仅是一种代码气味(也是一种刺鼻的气味),而且还会跳过所有可能引起您对错误的关注的错误。

Second, never declare multiple variables on one line. 其次,永远不要在一行上声明多个变量。 Just because you can, doesn't mean you should. 仅仅因为可以,并不意味着您应该这样做。

Third, you need to qualify your range references. 第三,您需要限定范围引用。

Cells(r, 9).Value is really ActiveSheet.Cells(r, 9).Value and guess what would happen if the ActiveSheet changed while the code was running? Cells(r, 9).Value实际上是ActiveSheet.Cells(r, 9).Value并猜测如果在代码运行时ActiveSheet发生更改会发生什么?

You are trying to (inefficiently) replicate with VBA what already exists very well in Excel. 您正在尝试使用VBA(低效)复制Excel中已经存在的很好的东西。 For example, open up a workbook and create a Table that spans from your start row to your end row (seems 4 to 1004 in this case). 例如,打开一个工作簿并创建一个从开始行到结束行的表(在本例中为4到1004)。 Enter your final VLOOKUP formula there. 在此输入您的最终VLOOKUP公式。 See how it either automatically fills to the last row, or gives you the option of overwriting all the cells in the column? 看看它是如何自动填充到最后一行,还是可以选择覆盖该列中的所有单元格? This is much faster than writing your formula in each row, and will work much better. 这比在每一行中编写公式快得多,并且效果会更好。

Additionally, since all of this data exists in other worksheets, you should have this data in tables, and if you do you can use those table names. 另外,由于所有这些数据都存在于其他工作表中,因此您应该在表中包含这些数据,如果可以, 可以使用这些表名。 For example, lets say each table was named the same as each sheet. 例如,假设每个表的名称与每个工作表的名称相同。 Your VLOOKUP would look something like: 您的VLOOKUP如下所示:

=VLOOKUP([KeyColumn], SPData, Column(SPData[TheDataYouWant]), False) and then, to overwrite errors, we make a slight modification: =VLOOKUP([KeyColumn], SPData, Column(SPData[TheDataYouWant]), False) ,然后,为了覆盖错误,我们进行了一些修改:

=IfError(VLOOKUP([KeyColumn], SPData, Column(SPData[TheDataYouWant]), False), "No Results")

Trust me, learning how to do this effectively first will help you be better at Excel. 相信我,学习如何做有效的第一将帮助你在Excel中会更好。

All you then need is three tables (notice how I say three, hint, hint ) where the first table has the data to calculate, the second table creates a key in the first column with that data, and the third looks it up. 然后,您只需要三个表(请注意我说的三个,hint,hint ),第一个表具有要计算的数据,第二个表在第一列中使用该数据创建键,第三个表进行查找。 All you then need to do is paste your raw data into your first table, and your populate the keys you're interested in for your third table, and voila! 然后,您需要做的就是将原始数据粘贴到第一个表中,然后为第三个表填充感兴趣的键,瞧!

Also, in case you missed it, your third VLOOKUP is trying to find the a key based off of the same key that your second VLOOKUP uses since a VLOOKUP requires the left-most value in a range to be the key for the lookup. 另外,如果您错过了它,则第三个VLOOKUP会尝试根据第二个VLOOKUP使用的相同键来查找该键,因为VLOOKUP要求将范围中最左边的值用作查找键。 If you can't do this, for whatever reason, learn INDEX/MATCH first. 如果由于某种原因您不能这样做,请先学习INDEX/MATCH

And please, until you cant reasonably solve the problem with Excel, avoid VBA. 而且,请您避免使用VBA,直到不能用Excel合理地解决问题为止。

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

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