2016-12-17 130 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