簡體   English   中英

Excell VBA數組循環在另一個循環內

[英]Excell vba array loop inside another loop

我嘗試從excel中的每一行讀取單元格,並檢查我的單元格是否包含數組中的值。

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

不幸的是,循環中的“元素”始終是最后一個array(products)元素。 在這種情況下為“ MS-CHOP-LR”。

1.不能用於數組中的每個數組。 采用;

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

或利用收藏集(Google是您的朋友)

2.代碼的最后一行應說明

Next x

3. LastRow不一定總能為您帶來正確的價值。 采用;

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

如果您確定您知道有一條conc科,或者

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

獲取該單元格給定范圍內的絕對最后一行。 請注意,在這兩種情況下,如果在該單元格下面都沒有任何值,則返回工作表的最后一行(xls 2003為65k,xlsx 2007+為1M)。 還有其他選項可以獲取范圍的最后一行,但這兩個是我最喜歡的。

4.如果else語句后面不需要雙列(:)

我花了幾個小時,但我想我終於明白了您在說什么,您的問題是……代碼運行后,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

如果我完全誤解了您的問題,請更新問題以提供更多信息。 (也許添加當前代碼正在產生的內容以及您期望它產生的內容的屏幕轉儲。)

有一個“不錯的竅門”來查找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

非常感謝您的快速回答。 我將使用Shai Redo解決方案:

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))

如何在“匹配”產品所在的地方使用這樣的數組? 問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM