Excel vLookup Returning 0

时间:2015-05-12 22:48:18

标签: excel vba excel-vba vlookup

I'm working with a two very large tables and I'm trying to use vba and vlookups to match the data in one with the other.

The worksheets are each one column and are structured like below:

Reference Sheet:
ID_Ref (2/4)
Q1234
W1234
R1234
...

Lookup Sheet
ID_Lookup
Q1234
P1234
X1234
...

I'm interested in identifying the rows that are in the Lookup SHeet but not in the Reference Sheet. I have a huge quantity of rows in each sheet (~130k in the Lookup and 90k in the Reference) and and excel kept crashing when I tried to enter the formulas manually.

As a result I'm trying to work with VBA to automate the process. The issue is that the below function returns 0 for most of the rows and I can't figure out why.

RegionNodes

Any help is appreciated!

EDIT 5/13:

I tried the methods mentioned by @HarveyFrench and @Jeeped and got two different results.

Copying down:

    Sub Vlookup()

    With Sheets("Lookup).Range("B3:B133780")
        .FormulaR1C1 = _
        "=IF(VLOOKUP(RC[-1],2_4!R1C1:R79145C1,1)=RC[-1],VLOOKUP(RC[-1],2_4!R1C1:R79145C2,1,FALSE),VLOOKUP(RC[-1],2_4!R1C1:R79145C2,1,FALSE))"
        .Value = .Value
    End With 
    End Sub

yields a different number of non-matching records than copying down

=NOT(ISNA(VLOOKUP($A2,'2_4'!$A$1:$A$79145,1, FALSE)))

Am I doing something wrong here?

3 个答案:

答案 0 :(得分:2)

Here is yoru formula, it looks dodgy!

foreach (block b in blocks) 
{
    YourFunction(b);
}

In the cells in the column to the right of the lookup column with values (which I will assume is column A and start in row 2. Try this formula in cell B2 then copy and paste it down:

'=IF(VLOOKUP(RC[-1],2_4!R1C1:R79145C1,1)=RC[-1]
    ,VLOOKUP(RC[-1],2_4!R1C1:R79145C2,1,FALSE)
    ,VLOOKUP(RC[-1],2_4!R1C1:R79145C2,1,FALSE))

This will return true when a value is found or false when it isn't.

I think you need to re-read the manual pages on VLOOKUP as you not using it right.

You might like to use the ribbon>>formula tab>>calculation options to turn of auto calcualtion whilst you are editing, but I don;t think it will be necessary.

Harvey

答案 1 :(得分:2)

A MATCH function is more efficient than a VLOOKUP function or even a COUNTIF function if all you want to do is prove existence.

    onclick="javascript:SelectStatus(thid.id);"

You will be left with blank cells and cells containing the row numbers of the match on worksheet '2_4' in column B.

答案 2 :(得分:1)

这里有一种不同的方法,使用ADO(改编自this answer,但修改为使用Excel 2007以后的连接字符串,并仅识别查找表中唯一的项目。)我已经假设查询表被调用"查找",参考表被称为"参考"我们识别的物品清单将输出到名为"数据" - 如果错误,请更换正确的名称。

在VBA编辑器中,转到工具>引用并添加对" Microsoft ActiveX Data Objects 2.8 Library"的引用。现在将其添加到标准代码模块,然后运行它:

ServicesComponents

这会尝试将查找表中的每个条目与参考表中的一个或多个相应条目进行匹配。然后它会消除所有匹配,并保留不匹配的查找条目。