2016-12-17 64 views
2

我尝试从excel中的每一行读取单元格,并检查我的单元格是否包含数组中的值。excell vba数组循环在另一个循环内

Dim products As Variant 
products = Array("MS-CHOPMAT-6", "MS-BOARDS-3", "MS-CHOP-LR") 
Dim element As Variant 

For x = 2 To LastRow 

    order_quantity = Range("$E$" & x).Value 
    item_price = Range("$F$" & x).Value 

    For Each element In products 

    If InStr(Range("$D$" & x), element) > 0 Then 
      Range("$H$" & x) = order_quantity * 3 

     Else: Range("$H$" & x) = "ERROR - " & order_quantity & element 

     End If 
    Next element  
Next 

不幸的是,循环中的“元素”始终是最后一个数组(元素)。在这种情况下“MS-CHOP-LR”。

回答

0

有一个“好招”找到,如果一个String是一个数组里面,它是通过使用Match功能。例如,假设您的单元格字符串为“MS-BOARDS-3”,则使用Match函数将返回一个数字值。

如果您的单元格字符串为“MS-ELSE”,则使用Match函数将返回错误,因为在您的数组中找不到它。所以,如果你添加一个If IsError(Application.Match(Range("$D$" & x).Value, products, 0)) Then你可以捕获这个场景,并直接弹出你想要的MsgBox

代码

Dim products As Variant 
Dim element As Variant 

' add an integer variable for the "Match" function 
Dim ArrElementID As Integer 

products = Array("MS-CHOPMAT-6", "MS-BOARDS-3", "MS-CHOP-LR") 

For x = 2 To LastRow 

    order_quantity = Range("$E$" & x).Value 
    item_price = Range("$F$" & x).Value 

    ' if value not found inside the array using the "MATCH" function 
    If IsError(Application.Match(Range("$D$" & x).Value, products, 0)) Then 
     Range("$H$" & x).Value = "ERROR - " & order_quantity & element 
    Else ' successful "MATCH" inside the array 
     Range("$H$" & x).Value = order_quantity * 3 
    End If 

Next 
+0

嗨Shai。如何在此代码中查找产品的索引? – awariat

+0

@awariat你问的是如何得到'产品'数组的索引?使用“匹配”功能? –

+0

没有如何找到你给我的这段代码中的产品索引。哪个位置被检查过? – awariat

0

1. 你不能在数组中使用每一个。使用;

For i = LBound(products) to UBound(products) 
... products(i) ... 
Next i 

或利用集合(谷歌是你的朋友)

2. 你的代码的最后一行应说明

Next x 

3. LASTROW可能不会总是得到你的正确的价值。使用;

Cells(x,y).end(xlDown).row 

如果您知道确保你有一个concatinae行或

Cells(x,y).SpecialCells(xlLastCell).roW 

要获得从该单元中给出的任何范围内的绝对最后一排。请注意,在这两种情况下,如果该单元格下面没有任何值,则返回工作表的最后一行(xls 2003和xlsx 2007+的某些内容为65k)。还有其他选项可以获得范围中的最后一行,但这两个是我最喜欢的。

4. 如果else语句不需要双列(:)后其他

+1

1.遍历Variant数组中的每个元素都行不通。 2.“下一个x”中的“x”是**可选的**(但对于跟踪哪个循环正在结束有用)。 3. OP的代码没有显示'LastRow'变量是如何设置的,所以他们可能使用了你提供的代码,或者可能更好的代码'Cells(Rows.Count,y).End(xlUp).Row' 4.自动生成包含else代码的Else行上的冒号(':')。 (从语法上讲,'Else'不应该有任何后面的代码,所以IDE插入了冒号语句分隔符来将行分成两条语句。) – YowE3K

0

我花了几个小时,但我想,我终于摸索出你说的话你的问题是...在代码运行后,H列中的每个单元格的值为order_quantity * 3或值为ERROR - xxxMS-CHOP-LR"

这是因为你正在经历元素products,你会发现无论是第一或第二元件上的比赛后,也因此会显示“错误”消息,每当最后一个元素不等于该产品在那一排。

我建议你改变你的代码如下:

Dim products As Variant 
products = Array("MS-CHOPMAT-6", "MS-BOARDS-3", "MS-CHOP-LR") 
Dim element As Variant 
Dim matched As Boolean 

For x = 2 To LastRow 
    order_quantity = Range("$E$" & x).Value 
    item_price = Range("$F$" & x).Value 

    matched = False 
    For Each element In products 
     If InStr(Range("$D$" & x).Value, element) > 0 Then 
      Range("$H$" & x).Value = order_quantity * 3 
      matched = True 
      Exit For 
     End If 
    Next element 
    If Not matched Then 
     Range("$H$" & x) = "ERROR - " & Range("$D$" & x).Value & " - unknown product" 
    End If 
Next 

如果我已经完全误解了您的问题,请更新的问题,以提供更多的信息。 (也许添加当前有哪些代码生成屏幕转储,你期望它产生什么。)

0

非常感谢您为您的快速解答。 我会用晒重做方案:

Dim products As Variant 
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row 
products = Array("MS-CHOPMAT-6", "MS-BOARDS-3", "MS-CHOP-LR") 
'products = Array(Array("MS-CHOPMAT-6", 11), Array("MS-BOARDS-3", 12), Array("MS-CHOP-LR", 13)) 
For x = LastRow To 1 Step -1 

order_quantity = Range("$E$" & x).Value 
item_price = Range("$F$" & x).Value 

' if value not found inside the array using the "MATCH" function 
If IsError(Application.Match(Range("$D$" & x).Value, products, 0)) Then 
    Range("$H$" & x).Value = "ERROR - " & order_quantity 
Else ' successful "MATCH" inside the array 
    Range("$H$" & x).Value = order_quantity * 3 & LastRow 
End If 

Next 

这是确定我的一个报告,但在另一个我需要阵列阵列像这样

products = Array(Array("MS-CHOPMAT-6", 11), Array("MS-BOARDS-3", 12), Array("MS-CHOP-LR", 13)) 

如何在“比赛”用这样的数组,其中产品是? Regards

+0

如果您接受了@ShaiRado的回答并删除了这个帖子,那么这个帖子的未来观众会更好。如果你有第二个问题,那么你应该发一个新帖子。这篇文章是关于使用'Match'函数的,而你的第二个问题更多的是像'Collection'这样的东西 – Ambie