[英]Replace #N/A with blanks in vlookup
I'm using a loop to update the values in one workbook from the values in another workbook using the following code: 我正在使用循环,使用以下代码从另一个工作簿中的值更新一个工作簿中的值:
Option Explicit
Sub DateFinder()
Dim rw As Long, x As Range
Dim extwbk As Workbook, twb As Workbook
' Turn off notifications
Application.ScreenUpdating = False
Set twb = ThisWorkbook
Set extwbk = Workbooks.Open("C:\Test.xlsx")
' Refresh UsedRange (get rid of "Ghost" cells)
Set x = extwbk.Worksheets("Sheet1").UsedRange
With twb.Worksheets("Sheet1")
For rw = 2 To .Cells(Rows.Count, "G").End(xlUp).Row
.Cells(rw, "Q") = Application.VLookup(.Cells(rw, "G").Value2, 4, False)
Next rw
End With
' Close workbook
extwbk.Close savechanges:=False
' Turn on screen updating
Application.ScreenUpdating = True
' Message Box showing that process is complete.
MsgBox "Done!"
End Sub
Everything works fine but I want to change the code to replace the #N/A results with blanks. 一切正常,但我想更改代码以将#N / A结果替换为空白。 I've search the site and found the following solutions but none of them are working for me: 我已经搜索了该站点,找到了以下解决方案,但是没有一个对我有用:
.Cells(rw, "Q") = IfError(Application.VLookup(.Cells(rw, "G").Value2, x, 4, False), "")
.Cells(rw, "Q") = "=IFERROR(Application.VLookup(.Cells(rw, "G").Value2, x, 4, False),"""",Application.VLookup(.Cells(rw, "G").Value2, x, 4, False))"
.Cells(rw, "Q") = "=IF(ISNA(VLOOKUP(.Cells(rw, "G").Value2, x, 4, False)),"""",(VLOOKUP(.Cells(rw, "G").Value2, x, 4, False)))
I get a compile error with all three solutions. 这三个解决方案均出现编译错误。 If anyone could help me with the correct formula that would be great. 如果有人可以用正确的公式帮助我,那将是很好的。 Thanks. 谢谢。
You may need to qualify IfError
as a member of the Application
class, try: 您可能需要使IfError
成为Application
类的成员,请尝试:
.Cells(rw, "Q") = Application.IfError(Application.VLookup(.Cells(rw, "G").Value2, 4, False), "")
Another alternative is to assign the result of Application.VLookup
to a variant (otherwise, Type Mismatch will raise on the assignment), and then check for error. 另一种选择是将Application.VLookup
的结果分配给一个变量(否则,类型不匹配将在分配上引发),然后检查错误。 This is how you can use the IIF
and IsError
functions to check for error & cast to empty string in case of error: 这是您可以使用IIF
和IsError
函数检查错误并在出现错误的情况下IsError
转换为空字符串的方法:
Dim val As Variant
val = Application.VLookup(.Cells(rw, "G").Value2, 4, False)
.Cells(rw, "Q") = IIF(IsError(val),vbNullString,val)
Surround the function with an IfError
. 用IfError
包围函数。 However, in the future rather than using vlookup
, I recommend Index-Match. 但是,将来我建议不要使用vlookup
,而是推荐使用Index-Match。 You can read about it here: 你可以在这里读到它:
https://www.deskbright.com/excel/using-index-match/ https://www.deskbright.com/excel/using-index-match/
Index/Match
is a combination of 2 functions that have a net result better and easier than vlookup
. Index/Match
是两个功能的组合,比vlookup
更好,更容易获得最终结果。
Also, if the time comes where you need to transpose data, you would need to use hlookup
instead of vlookup
. 此外,如果时间一到,你需要转置的数据,你就需要使用hlookup
,而不是vlookup
。 Index Match takes care of everything for you. 索引匹配为您处理一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.