Visual Basic VLOOKUP

时间:2017-07-19 02:58:08

标签: excel-vba vlookup vba excel

我想自动运行以下声明:

= VLOOKUP(B8,Sheet 2中d:H,5,FALSE)

但我在第6行遇到错误:无法获取WorksheetFunction类的VLookup属性

Sub Test()
    Dim rng As Range
    Dim i As Long

    With ActiveSheet
        Set rng = .Range("B8:D" & .Cells(.Rows.Count, 1).End(xlUp).Row)

        For i = 18 to rng.Rows.Count
             rng.Cells(8, 6) = 
 Application.WorksheetFunction.VLookup(.Cells(i,1), Sheets("Sheet2").Range("D:H"), 5, False)
        Next
    End With
End Sub

我希望通过查找Sheet2

将输出放在SheetT F8上

1 个答案:

答案 0 :(得分:0)

怀疑你想要

Sub Test()
    Dim i As Long

    With ActiveSheet
        For i = 8 to .Cells(.Rows.Count, "B").End(xlUp).Row
            .Cells(i, "F").Value = Application.VLookup(.Cells(i, "B").Value, _
                                                       Sheets("Sheet2").Range("D:H"), _
                                                       5, _
                                                       False)
        Next
    End With
End Sub

您的原始代码是:

  • 始终写入rng.Cells(8, 6),这是一个B8偏移7行5列的单元格,即G15(或早期版本中的C15)< / LI>
  • 使用ActiveSheet.Cells(i, 1)作为查找值,这是A列中的单元格