简体   繁体   English

VBA,使用查找时出错

[英]VBA, error while using lookup

I am having two Sheets BW and PSW. 我有两个Sheets BW和PSW。

With the BW the data is starting from row 4. the above 3 rows are merged and I have some command Buttons there. 使用BW,数据从第4行开始。以上3行已合并,并且我在其中有一些命令按钮。 the sheet PSW starts from 1st row. 表格PSW从第一行开始。

I am looking for the ID in sheet1,which starts from L5 and when the Id matches in sheet2, it pulls the date from sheet2 to sheet1. 我正在寻找工作表1中的ID,该ID从L5开始,当ID在工作表2中匹配时,它将日期从工作表2拉到工作表1。 I am using the below code. 我正在使用以下代码。 The code, is not executing any Output. 该代码未执行任何输出。 Could anyone tell where I am making mistake. 谁能说出我在哪里犯错。

I suppose,in the below line, the range starts from A and since my data starts from row5, it does not Count. 我想在下一行中,范围从A开始,并且由于我的数据从row5开始,因此它不计数。 if this is the case, then how i should Change the range. 如果是这种情况,那么我应该如何更改范围。 I had this code running, if my data starts with row1 without any command buttons. 如果我的数据以没有任何命令按钮的row1开头,则我正在运行此代码。

totalrows = Sheets("BW").Cells(Rows.Count, "A").End(xlUp).Row totalrows = Sheets(“ BW”)。Cells(Rows.Count,“ A”)。End(xlUp).Row

Sub lookupePSR()
Dim totalrows As Long, totalrowsSht2 As Long
totalrows = Sheets("PSW").Cells(Rows.Count, "A").End(xlUp).Row
totalrowsSht2 = Sheets("PSW").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("PSW").Range("AA5:AA" & totalrows).Formula = Application.WorksheetFunction.IfError(Application.VLookup(Sheets("PSW").Range("L5:L" & totalrowsSht2), Sheets("PSW").Range("$A:$L"), 7, 0), "")
End Sub

As mentioned in comments, put the actual formula into the worksheet; 如评论中所述,将实际公式放入工作表中。 optionally revert to the values returned by the formulas. (可选)还原为公式返回的值。

with Sheets("PSW").Range("AA5:AA" & totalrows)
    .Formula = "=iferror(vlookup(l5, $a:$g, 7, false), text(,))"
    'optionally revert the formulas' returned values to values in cells
    .value = .value
end with

The TEXT(,) is the same as "" ; TEXT(,)""相同; ie a zero-length string. 即长度为零的字符串。 I use this because you have to double-up double-quotes (eg "" becomes """" ) when used in a quoted string like the formula above and that confuses matters. 我之所以使用它,是因为当在带引号的字符串(如上面的公式)中使用时,您必须将双引号加倍(例如""变成"""" ),这会引起混淆。

I've cut your lookup range down to A:G since you are only looking for the seventh column in the range. 由于您只在查找范围中的第七列,因此将您的查找范围缩小为A:G。

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

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