如何使用数组从另一个数组中获取值?

时间:2017-10-23 20:59:02

标签: excel vba excel-vba

我正在尝试在我的Excel工作表中搜索这些行,并查看该单元格是否显示了我的数组中的某个内容。如果它匹配我的数组中的一个值,我希望它进入另一个数组中的正确数字,然后拉出该值并放入另一个单元格。

所以我希望它在我的第一个数组中找到这个词,找出数组中的位置,然后从Array2获取相应的值并将该值放入所需的单元格中。这是我到目前为止,但我收到了

的错误信息
If ws1.Cells(lngRow, "F").Value = "" And _
            InStr(1, LCase(Range("J" & lngRow)), LCase(v)) <> 0 Then

它给我一个“运行时错误'424':需要对象”错误。所以我不确定我做错了什么。也许我根本没有正确地执行代码,但我认为它是这样的。

现在阵列中可能没有相同数量的信息,因为我将信息缩小以使其变小但我的真实值具有相同数量的值。

Dim lngRow As Long
Dim lngRows As Long
Dim arr As Variant, v As Variant
'
'    This function will search for all Transfers.
'    If the reason for action is ERROR, it will leave it. If it part of the list that needs changed, then it will make it blank or ""
'    Otherwise it will leave it as it is.
'
'
ActiveSheet.AutoFilterMode = False


arr = Array("Atlanta", "Chicago", "Crown Point", _
    "Dallas", "DC", "Equipment Company", "Denver", _
    "Detroit", "Home Office", "Houston", "Kansas", _
    "Las Vegas", "Louisville")

arr2 = Array("US Distribution", "US Products", "US Distribution", "US Distribution", "US Distribution", "US Distribution", "Equipment", "US Distribution", "US Products", "US Distribution", "Corporate", "US Distribution", "US Products", "US Distribution", "US Distribution", "US Distribution", "US Products", "US Products")

lngRows = Range("C" & Rows.Count).End(xlUp).Row


For lngRow = lngRows To 2 Step -1

    For Each v In arr
        If ws1.Cells(lngRow, "F").Value = "" And _
            InStr(1, LCase(Range("J" & lngRow)), LCase(v)) <> 0 Then
            ws1.Cells(lngRow, "I").Value = arr2(v)
        End If
    Next v

Next

End Sub

以下编辑代码

Dim wb As Workbook
Dim ws1 As Worksheet
Dim FFwb As Workbook
Dim FFws As Worksheet

Set wb = ActiveWorkbook
Set ws1 = wb.Sheets(1)

Dim lngRow As Long
Dim lngRows As Long
Dim arr As Variant, var As Variant
Dim arr2 As Variant
Dim locIdx As Variant

For locIdx = LBound(arr) To UBound(arr)
    var = arr(locIdx)
    For lngRow = lngRows To 2 Step -1
        For Each var In arr
            If ws1.Cells(lngRow, "F").Value = "" And _
                InStr(1, LCase(ws1.Cells(lngRow, "I").Value), LCase(var)) <> 0 Then
                ws1.Cells(lngRow, "H").Value = arr2(var)
            End If
        Next var
    Next lngRow
Next locIdx

这是我下面的工作代码。如果一个单元格是#N / A,则它不起作用,但它们不应该是#N / A.我不得不将arr2从最后一行拉到locIdx而不是var。

Dim locIdx As Variant


For lngRow = lngRows To 2 Step -1



    For locIdx = LBound(arr) To UBound(arr)
        var = arr(locIdx)
        If ws1.Cells(lngRow, "F").Value = "" And _
            InStr(1, ws1.Cells(lngRow, "I").Value, var) <> 0 Then
            ws1.Cells(lngRow, "H").Value = arr2(locIdx)
            'ElseIf LCase(ws1.Cells(lngRow, "A").Value) = "transfer ft<>pt" And _
                'InStr(1, LCase(Range("B" & lngRow)), LCase(v)) <> 0 Then
                'ws1.Cells(lngRow, "B").Value = ""
        End If
    'If (LCase(ws1.Cells(lngRow, "A").Value) = "transfer" And _
    '        InStr(1, LCase(Range("B" & lngRow)), LCase("err")) <> 0) Then
    '        ws1.Cells(lngRow, "B").Value = "Error"
    'End If

    Next locIdx

Next lngRow

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

v不是访问arr2的有效索引。您需要一个公共索引变量来从一个数组引用另一个数组。

您切换引用单元格的方法,这令人困惑。最好坚持一种方法,例如明确使用ws1.Cells.Value属性。这可能是您的错误的根源,这很难说。我不能说我喜欢使用字母作为列,但它是允许的。

Dim locIdx As Long
For locIdx = LBound(arr) To UBound(arr)
    v = arr(locIdx)
    If ws1.Cells(lngRow, "F").Value = "" And _
        InStr(1, LCase(ws1.Cells(lngRow, "J").Value), LCase(v)) <> 0 Then
        ws1.Cells(lngRow, "I").Value = arr2(locIdx)
    End If
Next locIdx

如果您的可能城市列表中包含连续字母&#34; dc&#34;,给定此处使用的字符串搜索,您可能会面临歧义问题。

答案 1 :(得分:0)

这是最终正常工作的固定代码。感谢eveyones的帮助。

Dim locIdx As Variant


For lngRow = lngRows To 2 Step -1



    For locIdx = LBound(arr) To UBound(arr)
        var = arr(locIdx)
        If ws1.Cells(lngRow, "F").Value = "" And _
            InStr(1, ws1.Cells(lngRow, "I").Value, var) <> 0 Then
            ws1.Cells(lngRow, "H").Value = arr2(locIdx)
            'ElseIf LCase(ws1.Cells(lngRow, "A").Value) = "transfer ft<>pt" And _
                'InStr(1, LCase(Range("B" & lngRow)), LCase(v)) <> 0 Then
                'ws1.Cells(lngRow, "B").Value = ""
        End If
    'If (LCase(ws1.Cells(lngRow, "A").Value) = "transfer" And _
    '        InStr(1, LCase(Range("B" & lngRow)), LCase("err")) <> 0) Then
    '        ws1.Cells(lngRow, "B").Value = "Error"
    'End If

    Next locIdx

Next lngRow