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

我试图通过我的Excel表格中的这些行进行search,并查看单元格是否在我的数组中显示其中的一个。 如果它匹配我的数组中的一个值,我希望它进入另一个数组到正确的数字,然后将该值拉到不同的单元格。

所以我希望它find我的第一个数组中的单词,找出数组中的位置,然后从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 

我在这里先向您的帮助表示感谢!

v不是访问arr2的有效索引。 你需要一个通用的索引variables来引用一个数组到另一个数组。

您切换引用单元格的方法,这是令人困惑的。 更好地坚持一种方法,例如明确使用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 

如果可能的城市列表中包含连续字母“dc”,则可能会遇到模糊问题,因为在此处使用了stringsearch。

这是最终工作的固定代码。 感谢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