[英]Vba keep previous cell value or skip if vlookup returns N/A
I have a large data set of countries and their cost of living index. 我拥有大量的国家/地区及其生活成本指数数据集。 They need to be updated quarterly by copying a table from a website.
需要通过从网站复制表格来每季度更新一次。
I made a macro to vlookup the updated index and replace the old one, but some entries no longer exist in the updated one or are not included. 我做了一个宏以vlookup更新索引并替换旧索引,但是某些条目不再存在于更新索引中或不包括在内。 It leaves the index cell with #N/A, but I rather just leave the old value.
它使索引单元格具有#N / A,但我宁愿保留旧值。
'Varibles and format
Dim last As Integer
Dim Ending As Integer
Dim examin As Variant
Ending = Cells(Rows.Count, "A").End(xlUp).Row
last = Range("G3").SpecialCells(xlCellTypeLastCell).Row
Range("F3:F" & last).ClearContents
Range("I3:M" & last).ClearContents
'Find & Replace country names with correct from
Cells.Replace What:="United States", Replacement:="USA", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="United Kingdom", Replacement:="England", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="United Arab Emirates", Replacement:="United_Arab_Emirates", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="Dominican Republic", Replacement:="Dominican_Republic", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="South Africa", Replacement:="South_Africa", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="Czech Republic", Replacement:="Czech_Republic", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="Costa Rica", Replacement:="Costa_Rica", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Vlookup updated index
For x = 2 To Ending
Range("D" & x).Value = Application.VLookup(Range("A" & x), Range("G3:H" & last), 2, False)
Next x
End Sub
I read on this question "How to keep previous excel cell value if VLOOKUP return with error" that it was not an option, but there might be a different way. 我读过这个问题“如果VLOOKUP返回错误时如何保持以前的excel单元格值”,这不是一个选择,但是可能会有不同的方式。
Here is what it looks like after I run it. 这是我运行它后的样子。
For x = 2 To Ending
If isnumeric(Application.VLookup(Range("A" & x), Range("G3:H" & last), 2, False)) then
Range("D" & x).Value = Application.VLookup(Range("A" & x), Range("G3:H" & last), 2, False)
End if
Next x
End Sub
Try that 试试看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.