2014-01-28 101 views
0

我有工作簿A中的值列表,我需要在已关闭的工作簿B中查找这些值。将工作簿中的单元格的值与已关闭的工作簿中的单元格进行比较

伪什么,我想实现:

For each entry x in Workbook A 
    Search through the column in Workbook B for entry x 
    Copy that value from Workbook B along with the value cell next to it 
Next entry 

我试图保存细胞作为变量的值,但我不知道如何来增加单元格的值,这样我可以继续在列表中移动。

我试过使用匹配功能,但我不知道如何正确比较值。

对此的任何帮助将是伟大的。

感谢, 伊恩

回答

0

您也可以尝试使用VLOOKUP:

Sub example() 
Dim ans As Variant 
Dim WB_B As Workbook 
Dim TWB As Workbook ' your workbook A 
Dim WB_B_rng As Range 
Set TWB = ThisWorkbook 
Set WB_B = Workbooks.Open("your workbook B's full path") 
Set WB_B_rng = WB_B.Sheets("workbookB's sheetname").Range("data table's range") 

TWB.Activate 
Sheets("workbookA's sheetname").Range("A1").Select ' or where your record's start located 
Do While Not IsEmpty(ActiveCell) 

ans = Application.WorksheetFunction.VLookup(ActiveCell.Value, WB_B_rng, 2, 0) 
ActiveCell.Offset(0, 1).Value = ans 
ActiveCell.Offset(1, 0).Activate 'goes down to the next cell until it's empty 
Loop 

WB_B.close 
End Sub 
+0

这好像就是我要找的,但是,当我运行从Excel的代码,下面的错误被抛出: '运行时错误 '1004':无法获取WorksheetFunction class'的VLOOKUP属性 –

+0

尝试使用Application.VLookup代替: ANS = Application.VLookup(ActiveCell.Value,WB_B_rng,2,0) – Alex

+0

即解决了这个问题。代码现在运行,但是B列中的所有引用(ActiveCell.Offset(0,1).Value = ans)都是无效引用(“#REF!”)。我不知道如何解决这个问题。我在B列中使用的工作簿B,这是我在WB_B_range中引用的那个值。 –

相关问题