用于vlookup的Excelmacros各种问题:将#n / a更改为零; 直接查找结果的查找值和粘贴值

我logging了一个vlookupmacros来获取r1c1公式,然后写了一个简单的macros来应用整个列范围的公式。 看下面的代码

信息:•我有一个对帐选项卡,列中列第6行中列出了唯一的ID。ID或行的数量是可变的,这意味着它可以随时更改,但目前大约有15000个ID。 这是一个全面的ID列表。 这些ID是一个连续的列表。 两者之间没有rest。 请记住,我在最后一个ID填充的下方有一个Grand Total Row。 •在同一工作簿中有第二张表,其中第A列中包含相同types的ID,第C至第C列中有不同的金额 – 第二张表称为“老年AR”。 由于此选项卡代表不同的报告types,因此并非所有ID都可以包含在此工作表中。

我已经开发了以下代码,在对帐选项卡上的D列第6行创build一个Vlookup,以拉入第二个工作表“Aged AR”的列C(第三列)中的金额,并将其应用于列中行的整个范围D的最后一个ID填充在A列。arrays或查找范围正在查找“Aged AR”选项卡上的列select所有列A到I,因为我将这个公式应用于范围从H6栏开始,并在“老年AR”表中的H栏中填入。

问题:1.如何扩展代码以将#N / A结果更改为零值? 2.如何将所有结果保留为格式化为值而不是离开查找公式? 3.在这个循环中,如何继续在第二列表“老化AR”上的查找数组集合中查找第8列的recon选项卡上从H6开始执行同一types的循环? 3.我预计这个循环会导致macros需要一段时间,因为一旦我的项目完成到六列,并且有很多行,我将会应用这种types的公式。 我工作簿中的所有参考表都包含相同types的ID。 是否有办法拉取引用的信息,并将其填充到我希望在“对帐”表中指定的位置,而无需使用查找公式。 它会使macros观工作更有效率吗?

我已经花了昨天和今天早上阅读不同的例子,但我没有能够注意到,我可以从中学习,因为我是一个完整的业余使用VBA

Sub findit() Dim oRange As Range Dim rec As Worksheet 'reconciliation tab contains ID's in column A Set rec = Sheets("Reconciliation") 'Located the end of the range I would like to populate in column D by finding the last row of IDs in column A Set oRange = rec.Range(rec.Range("D6"), rec.Range("A6").End(xlDown).Offset(0, 3)) 'apply the vlookup formula to using each ID as a lookup value in column A and populating the result in column D for as far down as the IDs are populated For Each cell In oRange 'vlookup formula is actually =vlookup(A6,'Aged Ar'!$A!:$I,3,false) 'where the lookup value A6 will increase to A7, A8,....n where n is the last row of IDs as it is applied to each row below starting at "D6" cell.FormulaR1C1 = "=VLOOKUP(RC[-3],'Aged AR'!C1:C9,3,FALSE)" Next cell End Sub 

您可以使用更高效的代码:

 With oRange .Formula = "=IFERROR(VLOOKUP(A6,'Aged Ar'!$A:$I,3,FALSE),0)" .Value = .Value End With 

而不是你的For Each循环:

 For Each cell In oRange 'vlookup formula is actually =vlookup(A6,'Aged Ar'!$A:$I,3,false) 'where the lookup value A6 will increase to A7, A8,....n where n is the last row of IDs as it is applied to each row below starting at "D6" cell.FormulaR1C1 = "=VLOOKUP(RC[-3],'Aged AR'!C1:C9,3,FALSE)" Next cell 

1)此代码将IFERROR / A结果更改为零值( IFERROR函数)

2)单元格格式化为值而不是离开查找公式( .Value = .Value语句)

3)这个代码将使macros观工作更有效率

4)对于相应的行,A6将增加到A7,A8,… n