仅当lookup_value包含值时,工作簿之间的VLOOKUP的Excel VBA宏

时间:2018-07-27 21:40:04

标签: excel excel-vba

我是vba的新手,所以请原谅我的菜鸟错误。

我假设用户已打开包含要查找的值的工作簿(在这种情况下为匹配连接器)。我想提示他们打开要为其添加包含配对连接器的列的工作簿。我在下面的代码中遇到的最大问题是,即使行一直空白到电子表格的限制(1,048,576行!),它也会插入vlookup。我只想在行包含E列中的值的情况下添加vlookup。

我还希望不要将vlookup中的工作表硬编码为特定工作表。我尝试创建变量来完成此操作,但是我无法在vlookup中找到使用它们的语法。

谢谢!

Sub Mate_OPEN_WKBOOK()

Dim xFileName As Variant
Dim xSourceSh As Worksheet
Dim xSourceWb As Workbook
Dim ws As Worksheet

MsgBox "Please open the workbook which you want to add mates to"
xFileName = Application.GetOpenFilename("Excel Files (*.xl*), *.xls", 1, "Open the workbook")
If xFileName = False Then Exit Sub
Set xSourceWb = Workbooks.Open(xFileName)
Set xSourceSh = xSourceWb.Worksheets.Item(1)
Set ws = ThisWorkbook.Sheets("List")

With xSourceSh
    Range("F:F").Formula = "=VLOOKUP('Lines'!B:B,'[Connector Mates SEARCH.XLS]List'!A:F,6,FALSE)"
    Range("G:G").Formula = "=VLOOKUP('Lines'!B:B,'[Connector Mates SEARCH.XLS]List'!A:F,4,FALSE)"

End Sub

0 个答案:

没有答案