运行时错误438 - 动态范围VLookup宏

时间:2017-11-03 18:20:44

标签: excel vba excel-vba vlookup

我想创建一个VBA宏来执行vlookup,其中数据源在一张纸上,而细胞群在另一张纸上。数据源和细胞群处于动态范围。以下是我到目前为止所得到的,并且我在" For Each cl in ws"中收到了运行时错误438。线。

'***Vlookup for a dynamic range where the range and cell population are on different sheets.***

Sub mylookup3()

Dim startCell As Range
Dim lastCol As Long
Dim ws As Worksheet
Dim lastRow As Long
Dim OppRow As Long
Dim OppClm As Long

'Dynamic ranges for my data source
Set ws = Sheet3
Set startCell = Range("C2")
    lastRow = ws.Cells(ws.Rows.Count, startCell.Column).End(xlUp).Row
    lastCol = ws.Cells(startCell.Row, ws.Columns.Count).End(xlToLeft).Column

    ws.Range(startCell, ws.Cells(lastRow, lastCol)).Select

'The cells I want to start populating
OppRow = Sheet2.Range("BL3").Row
OppClm = Sheet2.Range("BL3").Column

'Vlookup
For Each cl In ws
    Sheet2.Cells(OppRow, OppClm) = Application.WorksheetFunction.Vlookup(cl, ws, 6, False)
    OppRow = OppRow + 1
Next cl

End Sub

0 个答案:

没有答案