将工作簿中的单元格值与已关闭工作簿中的单元格进行比较

时间:2014-01-28 22:06:48

标签: excel-vba excel-2007 match vba excel

我在工作簿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

我试图将单元格的值存储为变量,但我不知道如何递增单元格值以便我可以继续向下移动列表。

我尝试过使用Match功能,但我不确定如何正确比较这些值。

对此的任何帮助都会很棒。

谢谢, 伊恩

1 个答案:

答案 0 :(得分: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