设置不同工作表和vlookup函数的范围

时间:2013-04-08 11:53:51

标签: vba excel-vba range vlookup excel

此代码正常运行:

Sub test_range()
Dim Range1 As Range
Set Range1 = Sheet4.Range("B2:F6081")
End Sub

当我将范围更改为变量时,它会给出错误1004:

Sub test_range()
Dim Range1 As Range
Set Range1 = Sheet4.Range(Cells(2, 2), Cells(6081, 6))
End Sub

我的“选择单元格”转换有什么问题?

此外,我想将它应用到vlookup函数:

For C = 2 To ColNumber

    C4 = C + 4
    For R = 2 To RowNumber
        Set Condition = Sheet1.Cells(R, 2)
        'MsgBox (Condition)
        Set Range1 = Sheet4.Cells(2, 2).Offset(6079, C + 3) 'B2 (row 2 column 2) : F6081 (row 6081 column 6)
        'MsgBox (Range1)
            arg1 = 5
            arg2 = False
        Sheet1.Cells(R, C4).Value = Application.WorksheetFunction.VLookup(Condition, Range1, 5, 0)
    Next R

Next C

我尝试将范围修改为

Range1 = Sheet4.Cells(2, 2).Offset(6079, C + 3)

范围功能有效,但vlookup功能(再次)给了我错误1004

我读了一些vlookup的案例,以及vlookup作为变体的每个昏暗结果。我也应该这样做吗?我不能把它的结果放到一个列的值上吗?

1 个答案:

答案 0 :(得分:1)

Cells表示ActiveSheet.Cells!因此,如果您的活动工作表不是Sheet4,Sheet4.Range(Cells(...会给您一个错误!

请改用Sheet4.Range(Sheet4.Cells

甚至更好,请使用Sheet4.Cells(2,2).Resize(6080,5)