对象变量索引匹配错误

时间:2016-01-13 21:32:16

标签: vba

有人可以帮我这个吗? 它可能很简单,但我看不到它。 提前谢谢。

对象变量或未设置块变量(错误91) (在索引/匹配行中)

Dim last_row_Datasheet As Long
Dim i As Long
Dim j As Long
Dim found_value As Range
Dim found_value1 As Variant
Dim artnr As Variant
Dim Range_Lookup As Variant
Dim Range_Lookup1 As Variant

For i = 2 To last_row_Datasheet

artnr = Workbooks("queries.xlsm").Worksheets("Data").Cells(i, 2)

Set Range_Lookup = Workbooks("queries.xlsm").Worksheets("artnr_package").Range("A2:A88")
Set Range_Lookup1 = Workbooks("queries.xlsm").Worksheets("artnr_package").Range("A2:A88")
found_value = Application.WorksheetFunction.Index(Range_Lookup, Application.WorksheetFunction.Match(artnr, Range_Lookup1, 0)).Address
found_value1 = Workbooks("queries.xlsm").Worksheets("artnr_package").Range(found_value).Offset(0, 6)

If ActiveSheet.Cells(i, 10) = "Not packed" Then
Workbooks("queries.xlsm").Worksheets("Data").Cells(i, 10) = found_value1
End If

Next i

2 个答案:

答案 0 :(得分:0)

试试这个

set found_value = Range_Lookup1.Find(artnr)
found_value1 = Workbooks("queries.xlsm").Worksheets("artnr_package").found_value.Offset(0, 6)

答案 1 :(得分:0)

我尝试了几个选项并决定使用数组。 我填充了2个阵列并合并了2个阵列并在其中搜索。 而且我认为它必须更快,请参阅下面的代码。

随时欢迎提出意见。

Dim art_data() As Variant
ReDim art_data(1 To lr - 1, 1 To 25)
art_data = Workbooks("queries.xlsm").Worksheets("Data").Range("A2:Y" & lr).Value

Dim art_package() As Variant
ReDim art_package(1 To lr1 - 1, 1 To 10)
art_package = Workbooks("queries.xlsm").Worksheets("artnr_package").Range("A2:J88").Value

For i = 1 To lr - 1

    For j = 1 To lr1 - 1

    If art_data(i, 2) = art_package(j, 1) Then
    ReDim Preserve art_data(LBound(art_data) To UBound(art_data), 1 To 35)
        art_data(i, 26) = art_package(j, 1)
art_data(i, 27) = art_package(j, 2)
art_data(i, 28) = art_package(j, 3)
art_data(i, 29) = art_package(j, 4)
art_data(i, 30) = art_package(j, 5)
art_data(i, 31) = art_package(j, 6)
art_data(i, 32) = art_package(j, 7)
art_data(i, 33) = art_package(j, 8)
art_data(i, 34) = art_package(j, 9)
art_data(i, 35) = art_package(j, 10)
    End If

    Next j

Next i

    For k = 1 To lr - 1

    If Trim(art_data(k, 10)) = "" Then
    ReDim Preserve art_data(LBound(art_data) To UBound(art_data), 1 To 35)
    art_data(k, 10) = art_data(k, 35)

    End If

    Next k