使用VLOOKUP引用VBA excel 2007中的另一个工作表

时间:2013-11-27 12:00:42

标签: excel vba excel-vba

我知道围绕这个主题已经提出了许多问题并得到了解答,但我找不到任何对我有用的东西,因为我正在引用另一张表(Lookuptable)。

下面是用excel编写的原始VLOOKUP,这很好用:

=VLOOKUP(A1,Lookuptable!A:B, 2, FALSE)

下面是我的脚本的剥离后的伪代码版本,其中提供了VBA vlookup代码行。我基本上都在寻找上面有效的VBA版本!

Sub Test()
    Dim rng As Range
    Dim result As Variant
    Dim i As Long
    Dim rng2 As Range
    Dim arg4 As Boolean
    Dim arg1 As Long, arg3 As Long

    With ActiveSheet
        Set rng = Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
    End With

rng.Cells(i, 2) = Application.WorksheetFunction.VLookup(i, Sheets(Lookuptable).Range("A1:B1"), 2, False)

End Sub

我可能值得注意的是,我以下的范围错误或无效的过程调用或参数错误。

1 个答案:

答案 0 :(得分:2)

试试这个:

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

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

        For i = 2 to rng.Rows.Count
            rng.Cells(i, 2) = Application.WorksheetFunction.VLookup(.Cells(i,1), Sheets("Lookuptable").Range("A:B"), 2, False)
        Next
    End With
End Sub