In the past year I have become quite skilled in Excel, and I am ready for the next step and start using 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.
vlookup
in worksheet1
using the data from worksheet2
vlookup
, is the new reference data to be used in a second vlookup
using the data of a different worksheet 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. The problem that I encounter is that the 3rd vlookup does not generate any results.
Also, the On Error Resume Next
works great but I would like to fill the blanks with "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.
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?
You are trying to (inefficiently) replicate with VBA what already exists very well in 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). Enter your final VLOOKUP
formula there. 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([KeyColumn], SPData, Column(SPData[TheDataYouWant]), False)
and then, to overwrite errors, we make a slight modification:
=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.
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. 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. If you can't do this, for whatever reason, learn INDEX/MATCH
first.
And please, until you cant reasonably solve the problem with Excel, avoid VBA.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.