简体   繁体   English

Excel宏中的Vlookup

[英]Vlookup in excel macro

I am trying to retrieve the value based on a key in another excel sheet but it does not seem to be retrieving it so I am wondering if my code is correct: 我正在尝试根据另一个Excel工作表中的键来检索值,但是似乎没有在检索它,所以我想知道我的代码是否正确:

Sheets("Details").Cells(rrow, 9).Value = Application.WorkSheetsFunc.VLookup(Cells(rrow, 4).Value, Sheets("Address").Range("G5693:G5843"), 3, False) 

Anybody know if the syntax is correct or is anything wrong with my code ? 有人知道语法是否正确或我的代码有问题吗?

Few suggestions: 几点建议:

Qualify the lookup value Cells(rrow, 4).Value with the sheet reference. 使用工作表引用限定查找值Cells(rrow,4).Value。 Which sheet this cell belongs to? 该单元格属于哪张纸? Otherwise it will refer to the activesheet only. 否则,它将仅引用活动表。 Also make sure rrow is not null in the code. 还要确保行在代码中不为空。

When retrieving the value through VLookUp, first check if the lookup value is found in lookup column and if yes, proceed for Vlookup to get the desired value from the desired column. 通过VLookUp检索值时,首先检查在查找列中是否找到查找值,如果是,请继续进行Vlookup从所需列中获取所需值。 Considering lookup_table is Range("A5693:L5843"), the lookup value must be present in the first column ie in this case Range("A5693:A5843"). 考虑到lookup_table为Range(“ A5693:L5843”),查找值必须出现在第一列中,即在这种情况下为Range(“ A5693:A5843”)。

If Application.CountIf(Sheets("Address").Range("A5693:A5843"), Cells(rrow, 4).Value) > 0 Then
    Sheets("Details").Cells(rrow, 9).Value = Application.WorkSheetsFunc.VLookup(Cells(rrow, 4).Value, Sheets("Address").Range("G5693:G5843"), 3, False)
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM