Excel中的VLookupmacros

我有两个工作表的Excel工作簿。 工作表A中包含多个名称,每个名称都包含在不同的列中,而工作表B包含工作表A中的相同名称和包含date的第二列。 例:

Worksheet A. Worksheet B. Name. Name. Dates Sean Jake 11/13/15 Jake Sean 10/11/14 Tom. Chris 12/12/15 

我正在尝试做的是设置一个调用VLookup的macros,并将工作表A中名称列的名称作为工作表B上的searchparameter passing。一旦在工作表B上find该名称,它将返回date。 目前,我正在通过在工作表A的列中硬编码以下vlookup来手动获取这些数据。

 =VLOOKUP(A2,'Worksheet B'!A:B,2,FALSE) 

任何build议和帮助,不胜感激。

谢谢。

您可以使用VBA中的工作表函数。 这个macros通过将他们发现的值返回给适当的单元格来利用它们。

 Sub auto_VLOOKUP() Dim rw As Long, wsB As Worksheet Set wsB = Worksheets("Worksheet B") With Worksheets("Worksheet A") For rw = 2 To .Cells(Rows.Count, 1).End(xlUp).Row If CBool(Application.CountIf(wsB.Columns(1), .Cells(rw, 1).Value)) Then ' VLOOKUP is typically used to return data from the right of the lookup column .Cells(rw, 2) = Application.VLookup(.Cells(rw, 1).Value, wsB.Columns("A:B"), 2, False) ' INDEX/MATCH function pairs are used to wider scope .Cells(rw, 3) = Application.Index(wsB.Columns("N"), Application.Match(.Cells(rw, 1).Value, wsB.Columns("A"), 0)) End If Next rw .Cells(2, 2).Resize(rw - 2, 1).NumberFormat = "m/d/yyyy" End With Set wsB = Nothing End Sub 

您将不得不编辑工作表名称,并调整与您在示例数据中提供的不同的列。

这不是vlookup,但它会得到你想要的结果。

 Sub Button1_Click() Dim ws As Worksheet, sh As Worksheet Dim Rws As Long, Rng As Range Dim c As Range, FndC As Range, shRng As Range Set ws = Sheets("Sheet1") Set sh = Sheets("Sheet2") Set shRng = sh.Range("A:A").SpecialCells(xlCellTypeConstants, 23) With ws Rws = .Cells(Rows.Count, "A").End(xlUp).Row Set Rng = .Range(.Cells(1, 1), .Cells(Rws, 1)) End With For Each c In Rng.Cells Set FndC = shRng.Find(what:=c, lookat:=xlWhole) If Not FndC Is Nothing Then c.Offset(0, 1) = FndC.Offset(0, 1) Else: c.Offset(0, 1) = "Not Found" Exit Sub End If Next c End Sub