更改“VLOOKUP”代码以在整个范围内运行Cell by Cell

时间:2015-03-05 00:43:01

标签: excel-vba vlookup vba excel

我想将此代码FindReplace_With_Offset_1更改为FindReplace_With_Offset_2

FindReplace_With_Offset_1在Col Range上运行并且运行正常

我需要FindReplace_With_Offset_2仅在Col范围中的每个Cell上运行,即我需要每个单元格都是自己的范围,当我运行它时,我为每个具有值{{1}的Cell获得#NAME? }}

由于

#N/A

代码2

Sub FindReplace_With_Offset_1()
Dim wsFR As Worksheet, wsT As Worksheet
Dim tLR As Long, i As Long

 Set wsT = ThisWorkbook.Worksheets("XXX")
Set wsFR = ThisWorkbook.Worksheets("ZZZ")


With wsT
     tLR = .Range("C" & .Rows.Count).End(xlUp).Row
      With .Range("B2:B" & tLR) 'The Offset Range
           .Value = _
           "=VLOOKUP(D2," & wsFR.Range("D1").CurrentRegion.Address(1, 1, , 1) & ",2,0)"
           .Value = .Value
      End With
End With
End Sub

1 个答案:

答案 0 :(得分:0)

也许是因为你试图让代码读取错误值,而对于excel,单元格值不是文本" #N / A&#34 ;,尝试使用IfError公式在所需的单元格上运行验证,如下所示:

If WorksheetFunction.IfError(aCell,"Error") = "Error" Then