2017-07-26 143 views
0

我有一個國家及其生活成本指數的大型數據集。他們需要通過從網站複製表格進行季度更新。Vba保留上一個單元格值或跳過,如果vlookup返回N/A

我做了一個宏來查看更新後的索引並替換舊的索引,但是一些條目不再存在於更新後的索引中,或者不包括在內。它使用#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 

我在這個問題上寫着「如何保持以前的Excel單元格值,如果VLOOKUP返回錯誤」,這是不是一種選擇,但也有可能是一個不同的方式。

下面是它運行後的樣子。

Index Update

回答

2
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 

試一下

+0

它的工作,謝謝! –

相關問題